我有两个类和一个如下所示的接口。 快速概要:界面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