Java中无参数构造函数和默认构造函数的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27654167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:47:47  来源:igfitidea点击:

Difference between a no-arg constructor and a default constructor in Java

javaconstructordefault-constructor

提问by amila isura

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();
   }
}

Does this invoke the default constructor of this class while creating Test object called cFrame?

在创建名为 cFrame 的 Test 对象时,这是否会调用此类的默认构造函数?

采纳答案by Elliott Frisch

The defaultconstructor is a no-args constructor that the Java compiler inserts on your behalf; it contains a defaultcall to super();(not supper()) which is the default behavior. If you implement any constructor then you no longer receive a default constructor.

default构造函数是一个无参数的构造函数,代表您的Java编译器插入; 它包含defaultsuper();(not supper())的调用,这是默认行为。如果您实现任何构造函数,那么您将不再收到默认构造函数。

JLS-8.8.9. Default Constructorsays (in part),

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.

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.

如果一个类不包含构造函数声明,则隐式声明一个没有形式参数和 throws 子句的默认构造函数。

如果声明的类是原始类 Object,则默认构造函数的主体为空。否则,默认构造函数只调用没有参数的超类构造函数。

回答by Abhishek

The default constructoris a constructor that the Java compiler adds to your code if no explicit constructor is available. The default constructor invokes the super class constructor with no args.

default constructor是,Java编译器添加到您的代码,如果没有明确的构造函数可用的构造函数。默认构造函数调用没有参数的超类构造函数。

If you have added your own constructor (no matter whether it's without parameters or with parameters) the compiler will not add the default constructor in this case.

如果您添加了自己的构造函数(无论是无参数还是带参数),在这种情况下编译器都不会添加默认构造函数。

回答by Code Geek

What is a default constructor ?

什么是默认构造函数?

It is a constructor that is added by the compiler if you have not defined a constructor.

如果您没有定义构造函数,它是由编译器添加的构造函数。

If your class has a constructor already then the compiler will not add the default constructor.

如果您的类已经有构造函数,那么编译器将不会添加默认构造函数。

So in your case you have the constructor,

所以在你的情况下你有构造函数,

public Test(){
     super();
     this.setSize(200,200);
     this.setVisible(true);
   }

So there is no default constructor now to be invoked by the JVM.

所以现在没有由 JVM 调用的默认构造函数。

回答by blueberry

Answer is No. Reference variable cFrame will call non-arg constructor Test(), not default constructor. While Test() constructor will further find and call non-arg constructor of JFrame class(Parent) and so on Every class must have at least one constructor. If not declared explicitly, java compiler provides a non-parameterised constructor, i.e, default constructor. This default constructor calls its parent class's non-parameterised constructor It initializes class variables to their default values.

答案是否定的。引用变量 cFrame 将调用非参数构造函数 Test(),而不是默认构造函数。而Test()构造函数会进一步查找和调用JFrame类(Parent)的非arg构造函数,依此类推,每个类都必须至少有一个构造函数。如果没有明确声明,java 编译器会提供一个非参数化的构造函数,即默认构造函数。此默认构造函数调用其父类的非参数化构造函数,它将类变量初始化为其默认值。

Eg:

例如:

Class Base {}

Class Derived extends Base {} // Default constructor of class "Derived" will automatically calls non-arg constructor of class "Base" and intialzes value to the variables

While non-arg constructor is defined by a programmer only. It can also intializes the variables. One more point to add here is that super() call is automatically added by the java compiler, if doesn't find super() in derived class.

而非 arg 构造函数仅由程序员定义。它还可以初始化变量。这里要补充的一点是,如果在派生类中没有找到 super(),java 编译器会自动添加 super() 调用。

Eg:

例如:

Class Base {

int y;
    public Base() {
    }
    public int getY() {
    return y;
    }

}

public class Derived extends Base {
 private int x;

 public Derived() { //super() will be automatically used
 }

 public int getX() {
 return x;
 }

 public void setX(int x) {
 this.x = x;
 }
}

Derived d = new Derived();
 System.out.println("Base x value => " + d.getX());

System.out.println("Base y value => " + d.getY());

Result:

Base x value => 0 // Default value to its primitive datatype(In this case: int)

Base y value => 0

回答by Raman Keswani

Answer to your question is No. Java won't provide a default constructor if you write any kind of constructor in class.

您的问题的答案是否定的。如果您在类中编写任何类型的构造函数,Java 将不会提供默认构造函数。

One difference between them is that the body of default constructor will always be empty whereas we can insert our own code in no-arg constructor.

它们之间的一个区别是默认构造函数的主体将始终为空,而我们可以在无参数构造函数中插入我们自己的代码。