English 中文(简体)
这一类别为什么有两座建筑商?
原标题:Why does this class have two constructors?

我看到这种幻灯片,旨在说明建筑商。 我现在混淆不清,因为它有两个建筑商,在第二个建筑中,他们的工作相同,接受将pa升至零。 为什么编码员需要重复<代码>,即:id = id;这一名称=名称;? 为什么这一类别甚至需要两个建筑商?

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;
      }
    }
问题回答

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个建筑商,因为它显示了建筑商超负荷的概念:

拥有1个以上的建筑商(名称和回归类型(建筑商的类别为违约回报类型),但具有不同的参数(不同的签名)

负荷过重的建筑或方法参数可在型号和参数号......以及甚至顺序上有所改动。

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..

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

它有2个建筑师,这样你就可以创造像学生这样的学生。

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

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

如前所述,这被称为建筑商超载。 这类似于超载功能,因为你可以有两种功能,名称相同,但签名不同。

举例来说,不提供《政府采购协议》将其设定为0.0(假定以后会改变)。 可能的情况是,某些未来方法取决于《政府采购协议》的价值,即:界定的<<>>,而这两家建筑商就是这样做的。

让我们承担学生升学的要求,像甘帕一样,在增加学生的同时,如果是这样,你会创造机会。

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

让我们承担某些班级的升学,在升学的同时,一定要打克帕,那么你的学生目标将是<代码>。 学生=新学生(6,“stud2”);,其中含蓄地将学生格格帕作为ZERO。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...