”工欲善其事,必先利其器。“—孔子《论语.录灵公》
首页 > 编程 > Java中的超级关键字

Java中的超级关键字

发布于2025-02-07
浏览:377

Java 中的 super 关键字

Java 中的 super 关键字是一个引用变量,用于引用直接父类对象。每当您创建子类的实例时,父类的实例都会隐式创建,并由 super 引用变量引用。

Java super 关键字的用法

Super Keyword in Java

  1. super 用于引用直接父类的实例变量。

我们可以使用 super 关键字访问父类的数据成员或字段。如果父类和子类具有相同的字段,则使用它。

class Animal {
    String color = "white";
}

class Dog extends Animal {
    String color = "black";

    void printColor() {
        System.out.println(color); // 打印 Dog 类的颜色
        System.out.println(super.color); // 打印 Animal 类的颜色
    }
}

class TestSuper1 {
    public static void main(String args[]) {
        Dog d = new Dog();
        d.printColor();
    }
}

输出:

black white

在上面的示例中,Animal 和 Dog 类都具有公共属性 color。如果我们打印 color 属性,它将默认打印当前类的颜色。要访问父属性,我们需要使用 super 关键字。

  1. super 可用于调用父类方法。

super 关键字也可用于调用父类方法。如果子类包含与父类相同的方法,则应使用它。换句话说,如果方法被重写,则使用它。

class Animal {
    void eat() {
        System.out.println("eating...");
    }
}

class Dog extends Animal {
    void eat() {
        System.out.println("eating bread...");
    }

    void bark() {
        System.out.println("barking...");
    }

    void work() {
        super.eat();
        bark();
    }
}

class TestSuper2 {
    public static void main(String args[]) {
        Dog d = new Dog();
        d.work();
    }
}

输出:

eating... barking...

在上面的示例中,Animal 和 Dog 类都具有 eat() 方法,如果我们从 Dog 类调用 eat() 方法,它将默认调用 Dog 类的 eat() 方法,因为优先级赋予局部方法。要调用父类方法,我们需要使用 super 关键字。

  1. super 用于调用父类构造函数。

super 关键字也可用于调用父类构造函数。让我们来看一个简单的例子:

class Animal {
    Animal() {
        System.out.println("animal is created");
    }
}

class Dog extends Animal {
    Dog() {
        super();
        System.out.println("dog is created");
    }
}

class TestSuper3 {
    public static void main(String args[]) {
        Dog d = new Dog();
    }
}

输出:

animal is created dog is created

注意:如果构造函数中没有 super()this(),编译器会在每个类构造函数中自动添加 super()。(待补充)

众所周知,如果不存在构造函数,编译器会自动提供默认构造函数。但是,它还会将 super() 作为第一条语句添加。(待补充)

另一个 super 关键字的示例,其中 super() 由编译器隐式提供。(待补充)

class Animal {
    Animal() {
        System.out.println("animal is created");
    }
}

class Dog extends Animal {
    Dog() {
        System.out.println("dog is created");
    }
}

class TestSuper4 {
    public static void main(String args[]) {
        Dog d = new Dog();
    }
}

输出:

animal is created dog is created

super 示例:实际应用(待补充)

让我们看看 super 关键字的实际应用。这里,Emp 类继承 Person 类,因此 Person 的所有属性都将默认继承到 Emp。为了初始化所有属性,我们正在从子类使用父类构造函数。通过这种方式,我们正在重用父类构造函数。

class Person {
    int id;
    String name;

    Person(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

class Emp extends Person {
    float salary;

    Emp(int id, String name, float salary) {
        super(id, name); // 重用父类构造函数
        this.salary = salary;
    }

    void display() {
        System.out.println(id   " "   name   " "   salary);
    }
}

class TestSuper5 {
    public static void main(String[] args) {
        Emp e1 = new Emp(1, "ankit", 45000f);
        e1.display();
    }
}

输出:

1 ankit 45000

参考:https://www.javatpoint.com/super-keyword

This revised output maintains the original meaning while using different wording and sentence structures. The "TBD" sections are left as they were in the original, as they represent areas that require further explanation or completion, not necessarily paraphrase.

最新教程 更多>

免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。

Copyright© 2022 湘ICP备2022001581号-3