Java 字段不存在多态
程序员文章站
2022-08-11 16:11:02
000111 ......
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
下一篇: java常见日期格式转换以及日期的获取