Java中的超级关键字
超变量引用直接父类实例。
超级变量可以调用直接父类方法。
super()充当直接父类构造函数,并且应该是子类构造函数的第一行。
调用覆盖方法的超类版本时,将使用super关键字。
示例
class Animal {
public void move() {
System.out.println("Animals can move");
}
}
class Dog extends Animal {
public void move() {
super.move(); // invokes the super class method
System.out.println("Dogs can walk and run");
}
}
public class TestDog {
public static void main(String args[]) {
Animal b = new Dog(); // Animal reference but Dog object
b.move(); // runs the method in Dog class
}
}输出结果
这将产生以下结果-
Animals can move Dogs can walk and run
热门推荐
10 圣诞祝福语简短小学
11 祖国七十华诞简短祝福语
12 老师送的祝福语简短
13 生日祝福语大全女生简短
14 祝女性生日祝福语简短
15 牛年女神节祝福语简短
16 情人表白祝福语简短大气
17 老公开业祝福语简短
18 官宣新年祝福语简短