首页 新闻 会员 周边 捐助

JAVA的方法覆盖与变量隐藏,求解代码的运行思路?

0
悬赏园豆:100 [待解决问题]
 1 class SuperClass {
 2     private int number;
 3 
 4 
 5     public SuperClass() {
 6         this.number = 0;
 7     }
 8 
 9 
10     public SuperClass(int number) {
11         this.number = number;
12     }
13 
14 
15     public int getNumber() {
16         number++;
17         return number;
18     }
19 }
20 
21 class SubClass1 extends SuperClass {
22     public SubClass1(int number) {
23         super(number);
24     }
25 
26 }
27 
28 class SubClass2 extends SuperClass {
29     private int number;
30 
31 
32     public SubClass2(int number) {
33         super(number);
34     }
35 
36 }
37 
38 public class SubClass3 extends SuperClass {
39 
40     private int number;
41 
42 
43     public SubClass3(int number) {
44         super(number);
45     }
46 
47 
48     public int getNumber() {
49         number++;
50         return number;
51     }
52 
53 
54     public static void main(String[] args) {
55         SubClass3 a3 = new SubClass3(20); 
56                 SuperClass s  = a3;  
57         SubClass1 a1= new SubClass1(20);
58                 SuperClass s1 = a1;
59         SubClass2 a2 = new SubClass2(20);
60                 SuperClass s2 = a2;
61         System.out.println(s.getNumber());
62                 System.out.println(a3.getNumber());
63         System.out.println(s1.getNumber());
64                 System.out.println(a1.getNumber());  
65         System.out.println(s2.getNumber());
66                 System.out.println(a2.getNumber());
67         
68     }
69 
70 }

运行结果:

1

2

21

22

21

22

 

求解代码的运行思路?

lubocsu的主页 lubocsu | 初学一级 | 园豆:46
提问于:2016-01-08 23:27
< >
分享
所有回答(2)
1

不能回答,只能顶贴啦

lovelubo | 园豆:202 (菜鸟二级) | 2016-01-09 00:13
1

jasondyoung | 园豆:424 (菜鸟二级) | 2016-01-09 21:08
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册