【发布时间】:2015-03-26 21:18:37
【问题描述】:
请允许我解释一下我的理解。
class P
{
P()
{
System.out.println("hi "+this);
/*which object of P is currently executing this constructor?
Is "this" here object referenced by q or some other object?*/
}
}
class Q extends P
{
Q()
{
super();
/*constructor of superclass called on this object(referenced by q) */
}
}
class R
{
public static void main(String args[])
{
Q q = new Q(); //constructor Q() invoked on object referenced by q
}
}
所以我的疑问是: 1. super() 在 P 的哪个对象上调用(因为不存在)。 2. P()中的“this”指的是什么?它和q是同一个对象吗?也就是说this=q?
【问题讨论】:
-
如果你问
super()是否调用了它自己的超类的super(),那么答案是肯定的(直到超类是Object)。 -
我已经编辑了我的问题。请重新检查
-
两天前,你问了一个非常相似的问题。您一遍又一遍地坚持没有
P的实例(除非您在之前提出问题时将其称为A)。我和其他人解释说存在P的实例,因为Q的任何实例也是P的实例。为什么你还坚持没有P的实例?提问后你不看答案吗? -
这意味着多态在这里起作用......因为我们能够使用子类引用访问超类构造函数(这当然也是超类引用)?
标签: java oop inheritance nested subclass