我有兩個類和一個如下所示的接口。 快速概要:界面Winterface,Class Big,Class Little擴展並實現Winterface。我不明白'這個'與超類一起使用
public interface Winterface {}
public class Big {
public int hello = 88;
public Big() {}
public void theMethod() {
System.out.println ("Big was here: " + (this instanceof Winterface) + ", " + this.hello);
}
}
public class Little extends Big implements Winterface{
private boolean hello = true;
public Little(){}
public void theMethod() {
super.theMethod();
System.out.println("Little was here: " + hello);
}
public static void main(String [] args) {
Little l = new Little();
l.theMethod();
}
}
當我在小執行主,我得到下面的輸出
大在這裏:真的,88 很少有人在這裏:真正
我的問題是,如何才能
1)(Winterface的這個例子)返回true,但是
2)this.hello be 88? 如果this.hello = 88,那麼this = Big,這不是Winterface的一個實例。
我不明白這是怎麼可能的,在此先感謝
編輯:謝謝大家對我現在明白了,「這」指的是小,這是一個很大並實現Winterface。由於該方法被稱爲super.theMethod(),所以可用的變量'hello'是Big中的變量,即使'this'指的是很少。
相關閱讀@ http://www.techrepublic.com/article/avoid-these-java-inheritance-gotchas/5031837 – 2012-07-31 15:52:17