Java 字段不存在多态
1 public class Main{ 2 public static void main(String[] args) { 3 A Class = new A(); 4 System.out.println(Class.a); 5 System.out.println(Class.getA()); 6 A AClass = new B(); 7 System.out.println(AClass.a); 8 System.out.println(AClass.getA()); 9 B BClass = new B(); 10 System.out.println(BClass.a); 11 System.out.println(BClass.getA()); 12 } 13 } 14 15 class A{ 16 public int a = 0; 17 public int getA(){ 18 return a; 19 } 20 } 21 22 class B extends A{ 23 public int a = 1; 24 public int getA(){ 25 return a; 26 } 27 }
Results:
0
0
0
1
1
1