this(...)将在同一类中调用另一个构造函数,而
super()将调用超级构造函数。如果
super()构造函数中没有,则编译器将隐式添加一个。
因此,如果两者都允许,您最终可能会
super两次调用构造函数。
示例(不要在参数中寻找含义):
class A { public A() { this( false ); } public A(boolean someFlag) { }}class B extends A { public B() { super(); } public B( boolean someFlag ) { super( someFlag ); } public B ( int someNumber ) { this(); // }}现在,如果调用
new B(5),将调用以下构造函数:
this( false);A() ---------------> A(false)^|| super(); || this();B() <--------------- B(5) <--- you start here
更新 :
如果您能够使用
this()并且
super()可能会得到如下结果:
( 注意 :这是为了说明 如果允许您这样做, 可能会出什么问题-幸运的是 , 这不是 要发生的事情 )
this( false);A() ---------------> A(false)^ ^| || super();| super( true ); <--- Problem: should the parameter be true or false? | || this(); |B() <--------------- B(5) <--- you start here
如您所见,您会遇到一个问题,即
A(boolean)可以使用不同的参数来调用构造函数,而现在您必须以某种方式决定应使用哪种构造函数。另外,其他构造函数(
A()和
B())可能包含现在可能无法正确调用的代码(即乱序等),因为对的调用
super(true )会绕开它们,而
this()不会。



