我查看了这个链接,但并不真正理解clone
的含义无论如何我的问题,所以我有一个类继承了另一个类的两个属性, 它继承的类有set / get方法和拷贝构造函数。
我想在继承类中实现一个拷贝构造函数(忽略代码中的注释)
public class Instructor extends Person{
//officeNumber represents the office number where instructors can be found
private String officeNumber;
//constructor allows user to define first and last name and office number in demo
public Instructor(String fName, String lName, String officeNumber) {
super(fName, lName);
this.officeNumber=officeNumber;
}
}
我想把复制构造函数放在这里,到目前为止我能做的就是这个,但是我不能只是将一个对象传入一个demo类中的另一个构造函数,我必须包含全名
public Instructor(String fName, String lName,Instructor object2) {
super(fName,lName);
officeNumber=object2.officeNumber;
}
//get method for field
public String getOfficeNumber() {
return officeNumber;
}
}
如果有任何帮助,这是它继承的类
public class Person {
//firstName represents the first name of a person
private String firstName;
//lastName represents the last name of a person
private String lastName;
//constructor allows programmer to define first and last name of object in demo
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
//copy constructor
public Person(Person object2) {
firstName = object2.firstName;
lastName = object2.lastName;
}
//get methods for fields
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
}
答案 0 :(得分:1)
请详细说明您的问题。
你可以像这样在教师中拥有一个复制构造函数,
此代码仅供理解。
public class Instructor extends Person {
public String getIname() {
return iname;
}
public void setIname(String iname) {
this.iname = iname;
}
private String iname;
public Instructor(Person p, String myinstructor) {
super(p);
this.iname = myinstructor;
}
public Instructor(Instructor clone) {
super(clone);
this.iname = clone.iname;
}
}