Java 中的 super
关键字是一个引用变量,用于引用直接父类对象。 每当创建子类的实例时,都会隐式创建父类的实例,实例由super
引用变量引用。 如果没有 super
或 this
,编译器会在类构造函数中隐式调用 super()
。示例:
package com.yiibai.demo;
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