Java中的无参构造函数和默认构造函数的区别 [英] Difference between a no-arg constructor and a default constructor in Java
问题描述
实际上,我不明白无参arg构造函数和默认构造函数之间的区别。
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?
Does this invoke the default constructor of this class while creating Test object called cFrame?
推荐答案
args构造函数,Java编译器代表您插入;它包含默认
调用 super();
(不是 supper c $ c>),这是默认行为。如果您实现了任何构造函数,那么您不再接收默认构造函数。
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。默认构造函数说明(部分)
如果类不包含构造函数声明,
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屋!