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 中秋祝福语简短带署名