类构造函数调用,输入为类名。

huangapple go评论122阅读模式
英文:

Class constructor call with class as input

问题

我正在处理一个类,并且通过将其作为输入调用了该类。

professor = new Professor(EEEE, AAAA, YEARS_90, DEP);

depProf.addProfessor(professor)

Professor类有一个构造函数为 (String, String, Integer, String),但是在测试中,我想从另一个地方使用 (Professor) 作为输入来调用它。

public Professor(String name, String surname, Integer yearOfBirth, String department) {
    this.department = department;
    Person curPerson = new Person(name, surname, yearOfBirth);
}

我如何创建一个接受 (Professor) 作为输入的 Professor 构造函数?如果这有任何意义的话。

我在考虑在调用第二个构造函数时调用第一个构造函数,但实际上不知道如何使其工作。

英文:

I'm working with a class and getting a call to this class with it as input.

 professor = new Professor(EEEE, AAAA, YEARS_90, DEP);

 depProf.addProfessor(professor)

Professor has a constructor of (String, String, Integer, String), but in a test I want to call it from another place with (Professor) as input.

public Professor(String name, String surname, Integer yearOfBirth, String department) {

    this.department = department;
    Person curPerson = new Person(name, surname, yearOfBirth);

}

How can I make a constructor of Professor(Professor)? If that make any sense.

I'm thinking about calling 1st constructor when second is called but don't actually know how to make it work.

答案1

得分: 5

你可以在同一个类中调用另一个构造函数,例如:

public Professor(String name, String surname, Integer yearOfBirth) {
   this.name = name;
   this.surname = surname;
   this.yearOfBirth = yearOfBirth;
}

public Professor(String name, String surname, Integer yearOfBirth, String department) {
  this(name, surname, yearOfBirth);
  this.department = department;
}

public Professor(Professor p) {
  this(p.name, p.surname, p.yearOfBirth, p.department);
}
英文:

You can call another constructor in the same class for example:

public Professor(String name, String surname, Integer yearOfBirth) {
   this.name = name;
   this.surname = surname;
   this.yearOfBirth = yearOfBirth;
}

public Professor(String name, String surname, Integer yearOfBirth, String department) {
  this(name, surname, yearOfBirth);
  this.department = department;
}

public Professor(Professor p) {
  this(p.name, p.surname, p.yearOfBirth, p.department);
}

答案2

得分: 1

你可以使用 this 来调用另一个构造函数。

public Professor(Professor professor) {
    this(professor.name, professor.surname, professor.yearOfBirth, professor.department);
}
英文:

You can use this to call another constructor.

public Professor(Professor professor) {
    this(professor.name, professor.surname, professor.yearOfBirth, professor.department);
}

答案3

得分: 0

如果我理解正确,您正在尝试使用复制构造函数。在C++中,这个构造函数会被默认生成,但是在Java中,您需要在您的Professor类中手动实现一个:

public Professor(Professor p) { 
    this(p.name, p.surname, p.yearOfBirth, p.department);
}
英文:

If I get this right, you are trying to use a copy constructor. This constructor is generated by default in C++, but you have to manually implement one in Java, in your Professor class :

public Professor(Professor p) { 
    this(p.name, p.surname, p.yearOfBirth, p.department);
} 

huangapple
  • 本文由 发表于 2020年9月11日 03:23:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/63836403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定