Java中无参数构造函数和默认构造函数的区别 [英] Difference between a no-arg constructor and a default constructor in Java
问题描述
其实我不明白无参数构造函数和默认构造函数有什么区别.
Actually I can not understand that what is the difference between a no-arg constructor and a default constructor.
import javax.swing.*;
public class Test extends JFrame {
public Test() {
super();
this.setSize(200,200);
this.setVisible(true);
}
public static void main(Sting[] arg) {
Test cFrame = new Test();
}
}
这会在创建名为 cFrame 的 Test 对象时调用该类的默认构造函数吗?
Does this invoke the default constructor of this class while creating Test object called cFrame?
推荐答案
default
构造函数是 Java 编译器代表您插入的无参数构造函数;它包含对 super();
(不是 supper()
)的 default
调用,这是默认行为.如果您实现了任何构造函数,那么您将不再收到默认构造函数.
The default
constructor is a no-args constructor that the Java compiler inserts on your behalf; it contains a default
call to super();
(not supper()
) which is the default behavior. If you implement any constructor then you no longer receive a default constructor.
JLS-8.8.9.默认构造函数说(部分),
如果一个类不包含构造函数声明,则隐式声明一个没有形式参数和 throws 子句的默认构造函数.
If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.
如果声明的类是原始类 Object,则默认构造函数的主体为空.否则,默认构造函数只会调用不带参数的超类构造函数.
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中无参数构造函数和默认构造函数的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!