<分区>
关于默认构造函数在java中的真正作用,我有些不明白。在关于对象创建的官方教程中:
All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler will reject the program.
在有关默认构造函数的文档中 (§8.8.9)
If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.
因此,即使是 Object 类的默认构造函数也有一个空主体。而且我知道默认构造函数NOT 将字段初始化为其默认值,因为它是编译器做的:
It's not always necessary to assign a value when a field is declared. Fields that are declared but not initialized will be set to a reasonable default by the compiler.
我不明白的是,如果我们不声明构造函数,默认构造函数到底做了什么?