2012-07-31 57 views
3

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

+0

相关阅读@ http://www.techrepublic.com/article/avoid-these-java-inheritance-gotchas/5031837 – 2012-07-31 15:52:17

回答

2

this只能是一个类。但this.hello是该类可访问的字段。

由于this只能是一个类这是一个Little具有父Big和你打电话时在其父的方法只能看到hello这是它认为实现Winterface

即Java支持方法的多态性,但不支持字段。

+0

虽然这并没有回答关于'Winterface'的问题。 – 2012-07-31 15:38:09

+0

那么Summerface呢? – Almo 2012-07-31 15:39:15

+1

@ S.L.Barth好的,澄清。 – 2012-07-31 15:46:26

2

lLittleLittleBig也实现了Winterface行为。
super是对父类的调用,因此使用父类的hello成员(即Big)。
您不是在做this.hello,而是super.theMethod(),它使用父级的类成员变量hello

UPDATE:
super.theMethod()调用在父类中的相应方法。在父类中,您访问父级的字段(它们也属于派生类,因为Little也是Big)。所以this.hello就是访问父类的代码的一部分。
你可以想像的Little的打印内存中,如下所示:

++++++++ 
+ Big + 
-------- 
+Little+ 
++++++++ 

所以Little有父母的所有成员变量即Big,并在代码中它的Big“编码区”内运行super.theMethod()运行。
正如彼得指出在他的回答,polymorhism不支持的方法,我希望这个过于简单化的描述有助于理解这个

+0

呵呵?我在super.theMethod()中做了这个。 'this'怎么能用同一个方法引用两个不同的东西呢? – 2012-07-31 15:52:43

+0

@ColeCanning:查看更新 – Cratylus 2012-07-31 16:02:23

1

这是因为this instanceof ...检查不使用静态(即编译时)类型(这是Big),但对象的(this“)动态运行时类型(即this.getClass()),这是Little在你的例子中。如果将使用静态类型,运营商将是非常没有意义的,因为我们将有:

Object obj = "foo"; 

if (obj instanceof Object) { /* always entered */ } 
/* but */ if (obj instanceof String) { /* never entered */ } 

静态,在编译时。所述instanceof操作者的目的是使运行时类型的测试,例如:

Object obj = /* whatever */; 

if (obj instanceof String) { 

    String str = (String)obj; // Cast cannot fail 
    ... 

} else if (obj instanceof Long) { 

    Long val = (Long)obj;  // Cast cannot fail 
    ... 
} 

注意,这种技术只应谨慎使用。

0

您的变量是Big和Little的一个实例。它是Little的直接实例,但由于Little从Big继承,instanceof运算符也将为Big返回true。

Little l = new Little(); 
System.out.println(l instanceof Little); // true, l is an instance Little 
System.out.println(l instanceof Big); // true, l is an instance of Little which inherits from Big 

你的其他误解(我假设)是'方法查找'是如何工作的。当你调用方法时,它会选择Little的方法实现。当你调用super.theMethod时,你明确地说过“调用Big的这个方法的版本”,然后在这个方法中使用Big的hello变量而不是Little的hello变量。

+0

我明白,但在这种方法中,'这'似乎是指向我和大小。我现在明白,这是一个小和一个大,并实现winterface – 2012-07-31 15:55:56

+0

'这'的类型取决于你称之为'这'。该对象本身当然是一个小的,但是那个时候运行的代码在Big中。因此,'this'在此时被假定为Big,因为您正在运行Big的代码。 Big没有Little的内置知识,所以不能把'this'当作一个小小的对象。 – Chris 2012-07-31 16:05:05

0

这里发生的事情是,当你定义在Little您的变量hello不会覆盖变量hello就是里面要定义一个新的变量helloLittle是躲在内Big变量helloBig。因此,在Big的范围内,hello将指的是整数值88,并且在Little的范围内,hello将指的是真。这些都是包含在您的对象中的不同变量,唯一的区别是您引用它们的范围。

像这里的其他人所说的那样,instanceof是比较对象的运行时类型(由this.getClass()返回的内容)的运算符。当在Big中,即使对象中的变量范围将引用Big,this仍为运行时类型Little,这就是为什么它是Winterface的实例。