Java如何使用super关键字?
当一个类从其他类扩展时,该类或通常称为子类的类将继承超类的所有可访问成员和方法。如果子类重写其超类提供的方法,则访问超类中定义的方法的方法是通过super关键字。
package org.nhooo.example.fundamental;
public class Bike {
public void moveForward() {
System.out.println("Bike: Move Forward.");
}
}在ThreeWheelsBike的moveForward()方法中,我们使用super.moveForward()调用overrised方法,该方法将从Bike类中打印消息。
package org.nhooo.example.fundamental;
public class ThreeWheelsBike extends Bike {
@Override
public void moveForward() {
super.moveForward();
System.out.println("Three Wheels Bike: Move Forward.");
}
public static void main(String[] args) {
Bike bike = new ThreeWheelsBike();
bike.moveForward();
}
}
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志