有关示例中java的隐式构造函数的详细信息

时间:2016-07-23 16:08:30

标签: java class constructor coupling

我想要澄清与Java中隐式构造函数相关的语句。我在an article中读到了这个陈述,但我需要更多关于它的细节和一个理解它的例子。

语句是:当在类A中定义并实例化类型B的变量时,进行隐式构造函数调用,例如,B b = new B()

1 个答案:

答案 0 :(得分:0)

它基本上说任何实例化的类都有一个隐式构造函数:

public class B {

    //constructor       
    public B() {
        //implicity constructor
    }

}

public class A {

    //constructor       
    public A() {
         Bb = new B(); //calls the constructor inside B during setup even if the constructor method does not exist within B an implicit constructor is made
    }

}
  

默认构造函数是自动构造函数   生成,除非您定义另一个构造函数。它初始化任何   未初始化的字段为其默认值。 link

当B从A实例化时,这个构造函数在创建过程中基本被调用。有关更具体的细节,您应该在不同的交换中询问堆栈溢出,也可以尝试程序员部分。