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 教师闺蜜祝福语简短