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();
}
}