7.10 1. Go to the construction method of A and complete the empty parameter construction of Object. 2.int I=7 3.setI(20) calls the setI() of the subclass. At this time, the I of the subclass is 60, and the I of the parent class is still 7. 4. Print this.i. At this time, in the parent class In the class, it is 7; (note: the variable depends on the actual caller, this is the current parent class; the method is overridden and calls the subclass) 5.int I=10 covers the original 60. 6, print this .i is 10 at this time.
60.60 Note: If you delete the I of the subclass, the I of the parent class is called by default 3. Call the setI of the subclass and assign it to the parent class. At this time, the parent class changes to 60. 5. There is no coverage at this time , this.i calls the I inherited from the parent class by default, which is 60.
The execution sequence is as follows:
A.i
和B.i
都是package-private的,B.i
没有OverrideA.i
, the two are independent;A.setI
和B.setI
都是public,B.setI
OverrideA.setI
, A在构造时调用的是B.setI
;B’s members outside the constructor are initialized in
super()
之后执行的,B.i
先是在super里被B.setI
and set to 60, and then B’s members are initialized to 10;Delete
int i = 10;
之后, 如果A和B处于同一个package,B.setI
访问的就是A.i
in B. When super() is used, A.i is set to 60 and there will be no change. Both prints are 60If I declare a member in the subclass that already exists in the parent class, will there be no error? It should at least be a warning.
I can only say that the question is too wicked. It is impossible to write like this in actual development. You will dig a hole for yourself.
7.10
1. Go to the construction method of A and complete the empty parameter construction of Object.
2.int I=7
3.setI(20) calls the setI() of the subclass. At this time, the I of the subclass is 60, and the I of the parent class is still 7.
4. Print this.i. At this time, in the parent class In the class, it is 7; (note: the variable depends on the actual caller, this is the current parent class; the method is overridden and calls the subclass)
5.int I=10 covers the original 60.
6, print this .i is 10 at this time.
60.60
Note: If you delete the I of the subclass, the I of the parent class is called by default
3. Call the setI of the subclass and assign it to the parent class. At this time, the parent class changes to 60.
5. There is no coverage at this time , this.i calls the I inherited from the parent class by default, which is 60.
On the importance of Degbug...