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();
}
}
热门推荐
2 送蛇的祝福语简短
3 祝福语简短古诗词
10 美好的成语祝福语简短
11 特别的端午祝福语简短
12 高考送考祝福语简短
13 青春告白祖国简短祝福语
14 夸赞老师祝福语搞笑简短
15 对离岗同事祝福语简短
16 月子小孩祝福语大全简短
17 高考祝福语简短10字
18 祝福语诗意文案简短