1 public class demo {
 2 public static void main(String[] args) {
 3     Fu f = new Zi();
 4     f.show();
 5     System.out.println(f.num);
 6     f.function();
 7 }
 8 static class Fu {
 9     public int num = 100;
10     public void show() {
11         System.out.println("show fu");
12     }
13     public static void function() {
14         System.out.println("function fu");
15     }
16 }
17 
18 static class Zi extends Fu {
19     public int num = 1000;
20     public int num2 = 200;
21     public void show() {
22         System.out.println("show zi");
23     }
24     public void method() {
25         System.out.println("method zi");
26     }
27     public static void function() {
28         System.out.println("function zi");
29     }
30 }
31 }

运行结果:
show zi
100
function fu

多态中的成员访问特点:
        A:成员变量
            编译看左边,运行看左边。
        B:构造方法
            创建子类对象的时候,访问父类的构造方法,对父类的数据进行初始化。
        C:成员方法
            编译看左边,运行看右边。
        D:静态方法
            编译看左边,运行看左边。
            (静态和类相关,算不上重写,所以,访问还是左边的)
            
        由于成员方法存在方法重写,所以它运行看右边。

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-02-05
  • 2021-08-07
  • 2021-10-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-09-19
  • 2021-05-20
  • 2021-06-02
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案