简体   繁体   English

Java中的默认构造函数?

[英]Default constructor in Java?

What is the purpose of the default constructor in java java中default构造函数的目的是什么

class Bike1 {
    Bike1() {
        System.out.println("Bike is created");
    }
    public static void main(String args[]){
        Bike1 b=new Bike1();
    }
}

The default constructor provides the default values to the objects.默认构造函数为对象提供默认值。 The java compiler creates a default constructor only if there is no constructor in the class.仅当类中没有构造函数时,java 编译器才会创建默认构造函数。

Your example provides a constructor,您的示例提供了一个构造函数,

Bike1(){System.out.println("Bike is created");}

which means you do not get a default constructor.这意味着您没有获得default构造函数。 A default constructor is inserted if you do not provide any constructor.如果您不提供任何构造函数,则会插入一个默认构造函数。 Finally, Bike1 is a no-args constructor with package level (or default) access permission and appears to display a message when an instance of Bike1 is created.最后, Bike1是具有包级别(或默认)访问权限的无参数构造函数,并且在创建Bike1的实例时显示一条消息。

Default constructor means that when you don't create any constructor for your class, the compiler automatically creates a default constructor (with no parameters) to your class at the time of compilation.默认构造函数意味着当你没有为你的类创建任何构造函数时,编译器会在编译时自动为你的类创建一个默认构造函数(没有参数)。

In your example you created a constructor.在您的示例中,您创建了一个构造函数。 The constructor does not create any objects, it initialize the object.构造函数不创建任何对象,它初始化对象。

Default constructors allow you to create objects with known, default settings and behavior.默认构造函数允许您创建具有已知默认设置和行为的对象。 If you call a constructor with arguments, you are creating a custom object.如果您使用参数调用构造函数,则您正在创建自定义对象。 But calling the default constructor will create objects with identical properties every time it is used.但是调用默认构造函数将在每次使用时创建具有相同属性的对象。

Typically, a default constructor with "no code" doesn't need any code;通常,“无代码”的默认构造函数不需要任何代码; it already has all the information it needs to create the object.它已经拥有创建对象所需的所有信息。

Remember that default constructor and a constructor with no-args are different.请记住,默认构造函数和没有参数的构造函数是不同的。 Since you are defining a constructor Bike1(){} here, the default constructor will loose it's scope and will not be generated automatically.由于您在这里定义了一个构造函数Bike1(){} ,因此默认构造函数将失去它的作用域并且不会自动生成。

The default constructor is the no-argument constructor automatically generated unless you define another constructor.默认构造函数是自动生成的无参数构造函数,除非您定义另一个构造函数。 It initialises any uninitialised fields to their default values... follow this link.. Java default constructor它将任何未初始化的字段初始化为它们的默认值......按照这个链接...... Java 默认构造函数

Default constructor have no arguments(parameters) and constructor name is same as class name.It will invoke at the time of object creation.默认构造函数没有参数(参数)并且构造函数名称与类名称相同。它将在创建对象时调用。

Example:例子:

class Display{类显示{

Display(){展示(){

System.out.println("Default Constructor"); System.out.println("默认构造函数");

} }

} }

class constructor{类构造函数{

public static void main(String args[]){公共静态无效主(字符串参数[]){

Display dis=new Display(); Display dis=new Display();

} }

} }

Output:输出:

Default Constructor默认构造函数

Because when the time of object creation default constructor will invoke automatically.因为当对象创建的时候默认构造函数会自动调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM