class OBJECT 中的默认构造函数有什么作用?
What does the default constructor in the class OBJECT do?
我是一个 Java 初学者,学习 Java 编译器规则如下:
- 如果 class 没有超 class,将其扩展为对象 class
- 如果class没有构造函数,添加一个默认的无参构造函数
- 如果构造函数的第一行不是"super()"或"this()",则添加"super()"调用超class.[=24=的默认构造函数]
我了解到,我们创建的所有对象都派生自超class对象。
我的问题是对象 class 中的构造函数在调用时会做什么?
编辑:我的问题是构造函数在 class 对象中做了什么?我知道 subclasses 默认调用 superclass 的构造函数。
例如,我有以下代码(我在其中显式扩展到 Object 并调用 super() 来说明编译器的作用)。我的问题是,对 super() 的调用有什么作用?
public class Person extends Object
{
private String name;
public Person(String n)
{
super();
this.name = n;
}
}
所有 Java class 没有任何明确的超级 class,从 Object
扩展,除了 Object
本身。无需在您的代码中明确说明,编译器会处理。
此外,构造函数中的 super()
调用将调用 Object
的构造函数。如果查看实现,您会发现 Object 根本没有任何构造函数,因此它将使用一个不执行任何操作的隐式空构造函数。诸如构造一个 vtable 以确保继承的方法在那里使用或初始化对象的监视器等细节不是构造函数的一部分,而是在调用 new
运算符时由 JVM 实现执行。
public Object() {
}
对象 class 几乎没有在大多数 class 中派上用场的方法。例如,臭名昭著的 toString()
是在对象 class 中实现的。此外,hashCode()
在那里实施。
我建议您深入查看 Object.class 文件,以了解每个 Java class.
中继承了哪些方法
关于您在顶部的 3 条规则,"good" 用于学术目的,但从未在现实中使用过。您永远不会看到显式的 extends Object
,只有在需要覆盖默认行为时才调用 super()
或 this()
。
此外,避免滥用继承并使用更多组合(实现接口),尽管这取决于每个用例。
For example, I have the following code (where I explicitly extend to
Object and call super()). My question is, what does the call do
super() do?
实际上,Object()
什么都不做。
现在你不应该减少为任何 subclass 构造函数调用 super()
(带参数或不带参数)的约束到你有一个不扩展 [=30 的 class 的情况=] 明确地。
在这种情况下,调用 super()
似乎并不是很有用。
但是一旦 class 扩展了不同于 Object
的 class,这个约束就真正有意义了,因为子构造必须首先应用它的父构造。
但是语言笼统地说明了这一点。直接扩展 Object
.
的 classes 也不例外
可能是因为,它使事情变得更简单并且这不是真正的约束,因为编译器为您添加了对超级默认构造函数的调用:super()
.
My question is, what does the call to super() do?
它调用 java.lang.Object
的默认构造函数。并从 Java Language Specification, #8.8.9
回答你似乎真正想问的问题
8.8.9. Default Constructor
If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:
The default constructor has the same accessibility as the class (§6.6).
The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class (§8.8.1, §15.9.2, §15.9.3).
The default constructor has no throws clauses.
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.
注意最后一段。
我是一个 Java 初学者,学习 Java 编译器规则如下:
- 如果 class 没有超 class,将其扩展为对象 class
- 如果class没有构造函数,添加一个默认的无参构造函数
- 如果构造函数的第一行不是"super()"或"this()",则添加"super()"调用超class.[=24=的默认构造函数]
我了解到,我们创建的所有对象都派生自超class对象。 我的问题是对象 class 中的构造函数在调用时会做什么?
编辑:我的问题是构造函数在 class 对象中做了什么?我知道 subclasses 默认调用 superclass 的构造函数。
例如,我有以下代码(我在其中显式扩展到 Object 并调用 super() 来说明编译器的作用)。我的问题是,对 super() 的调用有什么作用?
public class Person extends Object
{
private String name;
public Person(String n)
{
super();
this.name = n;
}
}
所有 Java class 没有任何明确的超级 class,从 Object
扩展,除了 Object
本身。无需在您的代码中明确说明,编译器会处理。
此外,构造函数中的 super()
调用将调用 Object
的构造函数。如果查看实现,您会发现 Object 根本没有任何构造函数,因此它将使用一个不执行任何操作的隐式空构造函数。诸如构造一个 vtable 以确保继承的方法在那里使用或初始化对象的监视器等细节不是构造函数的一部分,而是在调用 new
运算符时由 JVM 实现执行。
public Object() {
}
对象 class 几乎没有在大多数 class 中派上用场的方法。例如,臭名昭著的 toString()
是在对象 class 中实现的。此外,hashCode()
在那里实施。
我建议您深入查看 Object.class 文件,以了解每个 Java class.
中继承了哪些方法关于您在顶部的 3 条规则,"good" 用于学术目的,但从未在现实中使用过。您永远不会看到显式的 extends Object
,只有在需要覆盖默认行为时才调用 super()
或 this()
。
此外,避免滥用继承并使用更多组合(实现接口),尽管这取决于每个用例。
For example, I have the following code (where I explicitly extend to Object and call super()). My question is, what does the call do super() do?
实际上,Object()
什么都不做。
现在你不应该减少为任何 subclass 构造函数调用 super()
(带参数或不带参数)的约束到你有一个不扩展 [=30 的 class 的情况=] 明确地。
在这种情况下,调用 super()
似乎并不是很有用。
但是一旦 class 扩展了不同于 Object
的 class,这个约束就真正有意义了,因为子构造必须首先应用它的父构造。
但是语言笼统地说明了这一点。直接扩展 Object
.
的 classes 也不例外
可能是因为,它使事情变得更简单并且这不是真正的约束,因为编译器为您添加了对超级默认构造函数的调用:super()
.
My question is, what does the call to super() do?
它调用 java.lang.Object
的默认构造函数。并从 Java Language Specification, #8.8.9
8.8.9. Default Constructor
If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:
The default constructor has the same accessibility as the class (§6.6).
The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class (§8.8.1, §15.9.2, §15.9.3).
The default constructor has no throws clauses.
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.
注意最后一段。