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 儿童毕业祝福语 简短6
14 开工仪式横幅祝福语简短
15 孩子日常祝福语大全简短
16 清晨寄语诗句祝福语简短
17 结婚送亲认亲祝福语简短
18 虎年喝酒拜年祝福语简短