Java super 关键字
实例
使用 super 调用 Dog 的超类(subclass):
class Animal { // Superclass (parent)public void animalSound() {System.out.println("The animal makes a sound");}}class Dog extends Animal { // Subclass (child)public void animalSound() {super.animalSound(); // Call the superclass methodSystem.out.println("The dog says: bow wow");}}public class Main {public static void main(String[] args) {Animal myDog = new Dog(); // Create a Dog objectmyDog.animalSound(); // Call the method on the Dog object}}
定义与用法
super 关键字指的是超类(父)对象。
它用于调用超类方法,并访问超类构造函数。
super 关键字最常用的用法是消除具有相同名称的方法的超类和子类之间的混淆。
要理解 super 关键字,您应该对 Java 的 继承性 和 多态性 有基本的了解。
关联页面
在 Java 继承 中学习更多关于继承(子类和超类)的知识。
在 Java 多态 中学习更多关于多态性的知识。