简体   繁体   English

在Java中将多个构造函数与一个对象一起使用

[英]Using multiple constructors with one object in Java

I have two constructors for Student and am trying to use both of them with one object. 我有两个用于Student的构造函数,正在尝试将它们与一个对象一起使用。 But I should be doing it wrong because my output is not what I expect it to be. 但是我应该做错了,因为我的输出不是我期望的。

Output: School: null Grade Level: 0 Intended Major: null 输出:学校:null等级级别:0预期专业:null

Student's ID number is: 154324 Student's name: Sam Bay Student's GPA: 3.56 学生的身份证号码是:154324学生的姓名:Sam Bay学生的GPA:3.56

Code for class definition: 类定义的代码:

public class Student
{
   private int id, gradeLevel;
   private String name, school, major;
   private double gpa;


   //constructor to initialize the instance variables of student object
   public Student(int id, String name, double gpa)
   {
      this.id = id;
      this.name = name;
      this.gpa = gpa;
   }

   public Student(int gradeLevel, String school, String major)
   {
      this.gradeLevel = gradeLevel;
      this.school = school;
      this.major = major;
   }    

   //toString() to display the attributions of the student object
   public String toString()
   {
      return "School: " + school +
             "\nGrade Level: " + gradeLevel +
             "\nIntended Major: " + major + "\n" +
             "\nStudent's ID number is: " + id +
             "\nStudent's name: " + name +
             "\nStudent's GPA: " + gpa;
   }

}//end class             

code for main: 主要代码:

public class StudentDrive

    {
       public static void main(String [] args)
       {
      //creating student objects
      Student sam = new Student(12, "Alpha High School", "Biology");
      sam = new Student(154324, "Sam Bay", 3.56);

      System.out.println(sam);

   }
}   

It seems like I've initialized the first part but I get null and 0??!!! 似乎我已经初始化了第一部分,但是我得到了null和0 ?? !!!!

You can't use two constructors simultaneously on a single object. 您不能在单个对象上同时使用两个构造函数。

In your code: 在您的代码中:

Student sam = new Student(12, "Alpha High School", "Biology");

creates a new Student object and assigns its reference to the variable sam . 创建一个新的Student对象,并将其引用分配给变量sam

On your next line: 在下一行:

sam = new Student(154324, "Sam Bay", 3.56);

This creates another Student object, separate from the first, and reassigns sam to refer to it instead. 这将创建一个与第一个对象分开的另一个 Student对象,并重新分配sam来引用它。 In the process, you end up orphaning the original Student and leave it open to garbage collection. 在此过程中,您最终会孤立原始的Student并将其留给垃圾回收。

What you really want to do is either pass all data required for by a Student through a single constructor, or provide getters/setters (eg setGradeLevel(int level) ) and a layer of exceptions that prevent methods from accessing a Student object until all fields are filled. 您真正想做的是要么通过单个构造函数传递Student所需的所有数据, 要么提供getter / setter(例如setGradeLevel(int level) )和一层阻止方法访问Student对象直到所有字段的异常层充满。 The first option is generally more sound. 第一种选择通常是声音更大。

For example, a complete constructor would look something like this (formatted for readability): 例如,一个完整的构造函数看起来像这样(为便于阅读而格式化):

public Student(int id, int gradeLevel, String name, 
               String school, String major, double gpa)
{
    // fill your fields in here
}

I think you should read through the docs for a constructor again ;) 我认为您应该再次通读文档以获取构造函数 ;)

With Student sam = new Student(12, "Oakton High School", "Biology"); Student sam = new Student(12, "Oakton High School", "Biology"); you are creating a Student-object with the given parameters and storing it in the variable sam. 您正在使用给定的参数创建一个Student对象,并将其存储在变量sam中。

When you call sam = new Student(154324, "Sam Bay", 3.56); 当您致电sam = new Student(154324, "Sam Bay", 3.56); you are again creating a new Student-object and storing it in sam. 您将再次创建一个新的 Student对象并将其存储在sam中。 You are not modifying the first object but rather discarding it and creating a new one. 您不是在修改第一个对象,而是丢弃它并创建一个新对象。

You should try adding a method to your Student object like: 您应该尝试向您的Student对象添加一个方法,例如:

public void setAdditionalValues(int id, String name, double gpa){
    this.id = id;
    this.name = name;
    this.gpa = gpa;
}

Hope this is helpful :) 希望这会有所帮助:)

EDIT: as mentioned earlier you could also use one constructor that takes all the arguments or implement setters for each attribute of the Student-object like this: 编辑:如前所述,您还可以使用一个构造函数,该构造函数接受所有参数或为Student-object的每个属性实现设置器,如下所示:

public void setName(String name){
    this.name = name;
}

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

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