Why does this class have two constructors?

隐身守侯 提交于 2019-12-21 17:02:58

问题


I see this in the slide that aims to illustrate constructors. I'm confused now because it has two constructors that have the same job accept setting gpa to zero in the second one. Why does the coder need to repeat this.id = id; this.name = name; again? Why does this class even need two constructors?

class Student{
      private int id;
      private String name;
      private double gpa;
      public Student(int id, String name, double gpa){
        this.id = id;  this.name = name;   this.gpa = gpa;
      }
      public Student(int id, String name){
        this.id = id;  this.name = name;   gpa = 0.0;
      }
      public boolean equals(Student other){
          return id == other.id && name.equals(other.name) 
                       && gpa == other.gpa;
      }
      public String toString(){
        return name + " " + id + " " + gpa;
      }
      public void setName(String name){
        this.name = name;
      }
      public double getGpa(){
        return gpa;
      }
    }

回答1:


As with most contrived examples, there is often no obvious reason other than to show that the overloading is possible. In this example, I would be tempted to refactor the second constructor like this:

 public Student(int id, String name){
    this( id, name, 0.0 );
  }



回答2:


There are 2 constructors as it shows the concept of constructor overloading:

Having more than 1 constructor(same name and return type(constructor has class type as its default return type)) but with different parameters(different signature)

parameters of overloaded constructors or methods can vary in type and number of parameters...and even the sequence

the instances of the class / objects that you create invokes constructors at time of creation.. so at that time you could provide 2 or 3 parameters depending upon which constructor you want to use.. if you provide 3 it uses 3 parameter constructor..and 2 parameters then it uses 2 parameter constructor

It is basically the need of sometimes providing gpa or sometimes not.. therefore having initialization of objects with different values..




回答3:


The constructor is overloaded (same name and return type with different parameters i.e different signature) so that you can initiate an instance of the class in different ways. One with a GPA that you choose and another one with a default GPA 0.0




回答4:


It has 2 constructors so you can create a Student like

Student s = new Student(1, "Bob");

or

Student s = new Student(1, "Bob", 4.0);



回答5:


As mentioned before, this is known as constructor overloading. It's similar to function overloading, in that you can have two functions with the same name, but different signatures, that do different things.

In this example, not providing a GPA sets it to 0.0 (presumably to be changed later). It could be the case that some future method depends on the GPA value being defined, and these two constructors achieve that.




回答6:


Let us assume student promotion has requirement like gpa should be present while adding student, in that case you will create

Student s = new Student(5,"stud1",4.0);

Let us assume some class promotions doesn't require gpa while promoting, then your student object will be Student s= new Student(6,"stud2"); which implicity set students gpa as ZERO.



来源:https://stackoverflow.com/questions/8859544/why-does-this-class-have-two-constructors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!