构造函数决定着类的初始化效率,而当在构造函数里面再执行其他构造函数(即new 其他类时),又会执行相关的初始化工作。
看如下极端的例子,在构造函数中再new 其他类导致死循环。
因此在实践开发中不要在构造函数中new 其他类。
public class EveryDay
{
public static void main(String[] args)
{
Son s = new Son();
s.doSomething();
}
}
class Father
{
public Father()
{
new Other();
}
}
class Son extends Father
{
public void doSomething()
{
System.out.println("doSomething");
}
}
class Other
{
public Other()
{
new Son();
}
}