点击运行
class Super_class { int num = 20; // 父类的display方法 public void display() { System.out.println("This is the display method of superclass"); } } public class Sub_class extends Super_class { int num = 10; // 子类的 display 方法 public void display() { System.out.println("This is the display method of subclass"); } public void my_method() { // 实例化子类 Sub_class sub = new Sub_class(); // 调用子类的 display() 方法 sub.display(); // 调用父类的 display() 方法 super.display(); // 打印子类的变量 num 的值 System.out.println("value of the variable named num in sub class:"+ sub.num); // 打印父类的变量 num 的值 System.out.println("value of the variable named num in super class:"+ super.num); } public static void main(String args[]) { Sub_class obj = new Sub_class(); obj.my_method(); } }
运行结果 :
正在执行...