【发布时间】:2017-08-31 18:49:12
【问题描述】:
class Super
{
Super()
{
System.out.println("This is Super Constructor");
}
}
class Sub extends Super
{
Sub()
{
//super() is automatically added by the compiler here!
System.out.println("This is Sub Constructor");
//super(); I can't define it here coz it needs to be the first statement!
}
}
class Test
{
public static void main(String...args)
{
Sub s2=new Sub();
}
}
输出:
这是超级构造函数
这是子构造函数
无论如何要这样做?
或者您不能在 Super() 之前访问 Sub()?
我知道先初始化超类或继承类,然后再初始化子类,这样做只是为了学习目的!
【问题讨论】:
-
如果您想要执行此操作,那么您需要查看您的对象模型。在这种情况下,您可能会错误地使用父子关系。
标签: java constructor subclass superclass super