Java的super关键字

super可以用来引用直接父类的实例变量

可以使用super关键字来访问父类的数据成员或字段。

如果父类和子类具有相同的字段,则使用super来指定为父类数据成员或字段。

Animal和Dog都有一个共同的属性:color。 如果我们打印color属性,它将默认打印当前类的颜色。要访问父属性,需要使用super关键字指定。

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

    void printColor() {
    
    
        System.out.println(color);//prints color of Dog class
        System.out.println(super.color);//prints color of Animal class
    }
}
public class TestSuper1 {
    
    
    public static void main(String[] args) {
    
    
        Dog d = new Dog();
        d.printColor();
    }
}

输出结果:

black
white

super可以用来调用直接父类方法

super关键字也可以用于调用父类方法。

如果子类包含与父类相同的方法,则应使用super关键字指定父类的方法。 换句话说,如果方法被覆盖就可以使用 super 关键字来指定父类方法。

Animal和Dog两个类都有eat()方法,如果要调用Dog类中的eat()方法,它将默认调用Dog类的eat()方法,因为当前类的优先级比父类的高。所以要调用父类方法,需要使用super关键字指定。

class Animal {
    
    
    void eat() {
    
    
        System.out.println("eating...");
    }
}
class Dog extends Animal {
    
    
    @Override
    void eat() {
    
    
        System.out.println("eating bread...");
    }
    void bark() {
    
    
        System.out.println("barking...");
    }
    void work() {
    
    
        super.eat();
        bark();
    }
}
public class TestSuper1 {
    
    
    public static void main(String[] args) {
    
    
        Dog d = new Dog();
        d.work();
    }
}

输出结果:

eating...
barking...

super()可以用于调用直接父类构造函数

super关键字也可以用于调用父类构造函数。

class Animal {
    
    
    Animal() {
    
    
        System.out.println("Animal is created.");
    }
}
class Dog extends Animal {
    
    
    Dog() {
    
    
        super();
        System.out.println("Dog is created.");
    }
}
public class TestSuper1 {
    
    
    public static void main(String[] args) {
    
    
        Dog d = new Dog();
    }
}

输出结果:

Animal is created.
Dog is created.

我们知道,如果没有构造函数,编译器会自动提供默认构造函数。

并且,它还添加了super()作为第一个语句。

下面是super关键字的另一个例子,Dog 类的构造函数和super()由编译器隐式提供。

class Animal {
    
    
    Animal() {
    
    
        System.out.println("Animal is created.");
    }
}
class Dog extends Animal {
    
    }
public class TestSuper1 {
    
    
    public static void main(String[] args) {
    
    
        Dog d = new Dog();
    }
}

输出结果:

Animal is created.

super实际使用示例

此部分建议结合 谈谈Java中的构造方法 阅读。

下面来看看 super 关键字的实际用法。

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);//reusing parent's constructor
        this.salary = salary;
    }
    void display() {
    
    
        System.out.println(id + " " + name + " " + salary + " ");
    }
}
public class TestSuper1 {
    
    
    public static void main(String[] args) {
    
    
        Emp e1 = new Emp(1,"ankit",45000f);
        e1.display();
    }
}

输出结果:

1 ankit 45000.0

在这里,Emp类继承了Person类,所以Person的所有属性都将默认继承到Emp。

要初始化所有的属性,可使用子类的父类构造函数。

这样,我们便重用了父类的构造函数。

猜你喜欢

转载自blog.csdn.net/qq_44491553/article/details/113823495