-4

我查看了此链接,但并没有真正理解克隆的含义

无论如何,关于我的问题,所以我有一个类从另一个类继承两个属性,它继承的类具有 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;
    }
}

我想把复制构造函数放在这里,到目前为止我所能做的就是这个,但我不能只将一个对象传递给演示类中的另一个构造函数,我必须包含全名

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;
    }
}
4

1 回答 1

1

请详细说明您的问题。

你可以像这样在 Instructor 中有一个复制构造函数,

此代码仅用于理解。

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;
    }
}
于 2013-07-19T04:20:58.620 回答