在Java中只要有子类继承父类,就可以使用 super 关键字引用父类的对象、方法。

相关实例:

//父类
class Super{
    int age = 20;
    String name = "陈陈菌";
}
//子类继承父类
public class Sub extends Super{
    int age = 3;
    String name = "小陈";
    public void getSuper(){
        //引用父类对象
        System.out.println("Super age = "+super.age+"| name = "+super.name);
    } 
    public static void main(String[]args){
        Sub ts = new Sub();
        System.out.println("Sub age = "+ts.age+"| name = "+ts.name);
        ts.getSuper();
    }
}

运行结果:

Sub age = 3| name = 小陈
Super age = 20| name = 陈陈菌