您还可以链接两个构造函数。请参阅构造函数链接:
http ://www.javabeginner.com/learn-java/java-constructors
public A()
{
this("default str", 0);
}
public A(String foo, int bar)
{
// more code here
}
(我认为这比 String[] args 更好,因为你可以很容易地混淆 args 的顺序,而且你只能处理字符串。(要处理不同的类型,你可以使用 Object[] args,但那更少可维护,因为它甚至不是类型安全的。)并且您希望控制参数的数量。)
请注意,通常有人希望以另一种方式链接,因此参数化构造函数会调用具有较少参数的构造函数。
public A()
{
// more code here
}
public A(String foo)
{
this.setFoo(foo);
// more code here
}
public A(String foo, int bar)
{
this(foo);
this.setBar(bar);
}
public A(String foo, int bar, float extra)
{
this(foo, bar);
this.setExtra(extra);
}