先放一段代码:
class Book {
int pages = 2;
protected int interviews = 5;
}
public class Magazine extends Book{
int interviews = 8; //Line out
private void totalPages() {
interviews = 1; //Line in
System.out.println("this : " + this.interviews);
System.out.println("super : " + super.interviews);
}
public static void main(String[] args) {
new Magazine().totalPages();
}
}
子类 Magazine 中有两行分别注释为:Line out 和 Line in;
情况一:
只注释掉 Line out
输出如下:
this : 1
super : 1
输出方法体内的interviews;
输出和 去掉this.和super. 结果一样
情况二:
只注释掉 Line in
输出如下:
this : 8
super : 5
分别输出各自的interviews;
此时 去掉this.和super. 的输出均为 8 ;
情况三:
同时注释掉 Line out 和 Line in;
输出如下:
this : 5
super : 5
即输出父类中的interviews
情况四:
均不注释掉
输出如下:
this : 1
super : 5
this输出方法体内的,
super输出父类的;
Q:
结合以上结果,情况四 和 情况一 均有 Line in,
区别在于 情况一 保留了 Line out,导致结果不同。
目前尚未搞清楚原因。