java 不带参数调用构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26534601/
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-11-02 10:06:04  来源:igfitidea点击:

Calling a Constructor without parameters

java

提问by Sylvester

I am trying to build a class with a constructor, mutators and accessors. Reading through books and online, I am made to learn that you can call a constructor with or without parameters. However, my case below seems not to work. I am not even able to compile without errors. It works when I use student jane = new student(""). What am I doing wrong?

我正在尝试使用构造函数、修改器和访问器构建一个类。通过阅读书籍和在线,我了解到您可以使用或不使用参数调用构造函数。但是,我下面的案例似乎不起作用。我什至无法在没有错误的情况下进行编译。当我使用student jane = new student("")时它有效。我究竟做错了什么?

Devolution.java:6: cannot find symbol
symbol  : constructor student()
location: class Governor
        student jane = new student();
                         ^
public class designers {

   public static void main(String[] args) { 

    student jane = new student();
    student smith = new student("jane", "36365355", "Nairobi", "Male");

    System.out.println("Janes's Properties: "+jane.name() + " " + jane.nationalID() + " " + jane.county()+"  "+jane.gender());
    System.out.println("Smith's Properties: "+smith.name() + " " + smith.nationalID() + " " + smith.county()+"  "+smith.gender());

   }    
 } 

other code is below

其他代码如下

public class student {

   //Private fields
   private String name;
   private String nationalID;
   private String county;
   private String gender;   

   //Constructor method
   public student(String name, String nationalID, String county, String gender)
   {
     this.name = name;
     this.nationalID = nationalID;
     this.county = county;
     this.gender = gender;

   }

   //Accessor for name
   public String name()
   {
     return name;
   }

   //Accessor for nationalID
   public String nationalID()
   {
     return nationalID;
   }

   //Accessor for county
   public String county()
   {
     return county;
   }

    //Accessor for gender
   public String gender()
   {
     return gender;
   }

 }

回答by chiastic-security

A constructor is a way of creating an instance of a class:

构造函数是一种创建类实例的方法:

Student s = new Student(...);

will create a new instance of the Studentclass and enable you to access it using s.

将创建Student该类的一个新实例并使您能够使用s.

Often, when you create an instance of a class, you need to specify certain information that's used in building the instance. In the case of a student, that might be the name, the age, and so on. You'd have a constructor that looks like this:

通常,当您创建类的实例时,您需要指定用于构建实例的某些信息。对于学生,这可能是姓名、年龄等。您将有一个如下所示的构造函数:

public Student(String name, int age) {
    //...
}

But in some contexts, you can build an instance of a class without needing (at least initially) to specify anything. So you might, for instance, have a constructor like this

但是在某些情况下,您可以构建一个类的实例而无需(至少在最初)指定任何内容。所以你可能,例如,有一个像这样的构造函数

public Student() {
    //...
}

which leaves the name and age fields blank or zeroed out, until you later set them with another method of the class.

这将 name 和 age 字段留空或清零,直到您稍后使用类的另一种方法设置它们。

The critical point for what you're doing is that you've made a constructor that requires various parameters, but you haven't specified one like this second example that doesn't require any. As things stand, you can write

您正在做的事情的关键点是您已经创建了一个需要各种参数的构造函数,但是您没有像第二个示例那样指定不需要任何参数的构造函数。就目前情况而言,你可以写

Student s = new Student("Bob", "ABC12345", "Surrey", "Male");

because you've got a constructor that takes four Strings as arguments. But you can't write

因为你有一个以四个Strings 作为参数的构造函数。但是你不能写

Student s = new Student();

because you didn't create a constructor that takes no arguments.

因为您没有创建不带参数的构造函数。

The slight wrinkle in this is that if you don't specify anyconstructors in your class, then Java will automatically createone for you that takes no arguments and doesn't do anything special. So if you don't write any constructors, you'll get one for free that looks like this:

这其中的一个小问题是,如果您没有在类中指定任何构造函数,那么 Java 将自动为您创建一个不带参数且不执行任何特殊操作的构造函数。因此,如果您不编写任何构造函数,您将免费获得一个如下所示的构造函数:

public Student() {
}

But that's onlyif you don't write any of your own. Since you've specified one that doestake parameters, Java won't give you a no-argument one for free. You have to put it in yourself if you want to be able to create instances without any arguments.

但是,这只是如果你不写任何你自己的。既然你已经指定一个带参数,Java将不会给你一个无参数的一个免费的。如果您希望能够在没有任何参数的情况下创建实例,则必须自己将其放入。

回答by Dawood ibn Kareem

You've only written one constructor - the one with four parameters. You don't have a constructor without parameters, so you can't write new student().

您只编写了一个构造函数——一个带有四个参数的构造函数。你没有没有参数的构造函数,所以你不能写new student().

Note that if you don't write any constructors at all, the compiler will automatically make a constructor for you, without parameters, but as soon as you write one constructor, this doesn't happen.

请注意,如果您根本不编写任何构造函数,编译器将自动为您创建一个不带参数的构造函数,但是只要您编写一个构造函数,就不会发生这种情况。

By the way, most people use capital letters for class names (so Student, not student). This makes it easy to distinguish them from the names of other identifiers. It would be good for you to get into the habit of doing the same.

顺便说一下,大多数人使用大写字母作为类名(所以Student,而不是student)。这使得很容易将它们与其他标识符的名称区分开来。养成这样做的习惯对您有好处。

回答by Eran

You don't have a constuctor without parameters in the studentclass. Such a constructor is generated by the compiler only if you haven't defined any other constructors, which you have.

student类中没有没有参数的构造函数。只有当您没有定义任何其他构造函数时,编译器才会生成这样的构造函数。

Just add the constructor :

只需添加构造函数:

   public student()
   {
       this.name = null;
       this.nationalID = null;
       this.county = null;
       this.gender = null;
   }

回答by frost287

Its called overloading the constructor. In your class, declare a constructor again without parameter requirements. See this postfor more info

它称为重载构造函数。在您的类中,再次声明一个没有参数要求的构造函数。查看此帖子了解更多信息

回答by Nabin

You need to make another constructor as follow:

您需要制作另一个构造函数,如下所示:

public Student(){
//do things here
}

Explanation:

解释:

When no constructors are defined in a class then there is a default constructor(without any parameters) already. In which case you don't need to define it. But if you have any constructor with some parameters, then you need to define the constructor without parameters as well.

如果在类中没有定义构造函数,那么已经有一个默认构造函数(没有任何参数)。在这种情况下,您不需要定义它。但是如果你有任何带参数的构造函数,那么你也需要定义不带参数的构造函数。

回答by Patricia Heimfarth

You don't have a constructor without parameters. That would only be the case when you had not write an own one. When you want to have the possibility to make an object of the class with or without parameters, you need two different constructors in your code.

你没有没有参数的构造函数。只有当您没有自己编写一个时才会出现这种情况。当您希望有可能创建带或不带参数的类对象时,您的代码中需要两个不同的构造函数。