Java克隆浅拷贝深拷贝构造函数嵌套对象

Java克隆浅拷贝深拷贝构造函数嵌套对象,java,clone,copy-constructor,deep-copy,shallow-copy,Java,Clone,Copy Constructor,Deep Copy,Shallow Copy,我已经编写了一个测试应用程序,用于演示使用浅层、深层和复制构造函数的Java克隆 我用浅薄和深刻的方法实现了目标,但用复制构造函数,我觉得我遗漏了一些东西 请查看下面的代码,让我知道复制构造函数实现的修复方法 public class CopyConstructorDemo { public static void main(String[] args) { Teacher teacher = new Teacher("Kripalu"); Stude

我已经编写了一个测试应用程序,用于演示使用浅层、深层和复制构造函数的Java克隆

我用浅薄和深刻的方法实现了目标,但用复制构造函数,我觉得我遗漏了一些东西

请查看下面的代码,让我知道复制构造函数实现的修复方法

public class CopyConstructorDemo {

    public static void main(String[] args) {

        Teacher teacher = new Teacher("Kripalu");
        Student sOrg = new Student(15007, "Amit", "Chirimiri", teacher);

        //Student sClo = sOrg;                          //Java Reference

        //Student sClo = (Student) sOrg.clone();        //CLONE


        Student sClo = new Student(sOrg);               //COPY CONSTRUCTOR

        sOrg.display();

        sClo.getTeacher().setName("ShriKrishn");

        sOrg.display();

    }


}

class Teacher implements Cloneable{

    String _name = "";

    Teacher(String name){
        this.setName(name);
    }

    String getName(){return _name;}
    void setName(String name){_name = name;}

    //For Deep copy
    //@Override
    protected Object clone(){

        try {
            return super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }

    }   
}

class Student implements Cloneable{

    int _rollNo;
    String _name;
    String _address;

    Teacher _teacher;

    Student(int rollNo, String name, String address, Teacher teacher){
        this.setRollNo(rollNo);
        this.setName(name);
        this.setAddress(address);

        _teacher = teacher;
    }

    Student(Student copyCons){
        this._rollNo = copyCons._rollNo;
        this._name = copyCons._name;
        this._address = copyCons._address;
        this._teacher = copyCons._teacher;

    }

    Teacher getTeacher(){return _teacher;}
    void setTeacher(Teacher teacher){_teacher = teacher;}

    int getRollNo(){return _rollNo;}
    String getName(){return _name;}
    String getAddress(){return _address;}

    void setRollNo(int rollNo){_rollNo = rollNo;}
    void setName(String name){_name = name;}
    void setAddress(String address){_address = address;}

    void display(){
        System.out.println(_rollNo+" "+
                           _name+" "+
                           _address+" "+
                           _teacher.getName());
    }

    @Override
    protected Object clone(){

        try {

            //return super.clone();     //For Shallow copy

            //For Deep copy
            Student cloned = (Student)super.clone();
            cloned.setTeacher((Teacher)cloned.getTeacher().clone());
            return cloned;

        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
            return null;
        }

    }



}
输出(复制构造函数)

15007 Amit Chirimiri Kripalu

15007 Amit Chirimiri ShriKrishn

编辑:

由于学生类包含嵌套类(教师)引用,简单的复制构造函数将不起作用。我们必须将克隆(浅拷贝)与学生类的拷贝构造函数一起用于教师,这里更改为拷贝构造函数

Student(Student copyCons){

    this._rollNo = copyCons._rollNo;
    this._name = copyCons._name;
    this._address = copyCons._address;
    this._teacher = (Teacher) copyCons._teacher.clone();  //FIX: thanks to Amir

}

其余代码相同。

复制构造函数和克隆方法应该是这样的:

对于学生:

 //Copy constructor for the student
 Student(Student copyCons){
    this._rollNo = copyCons._rollNo;
    this._name = copyCons._name;
    this._address = copyCons._address;
    this._teacher = copyCons._teacher.clone();
}

 //Clone for the student
 protected Student clone(){
     return new Student(this);
 }
对于教师:

//This is the copy constructor
Teacher(Teacher t){
        setName(t.getName());
 }

//That's how you clone an object of type teacher
protected Teacher clone(){
   return new Teacher(this);
}
使用示例:

Teacher t1 =  new teacher("Teacher 1");
Teacher t1Clone = t1.clone();

Student s1 = new Student(15007, "Amit", "Chirimiri", t1);
Student s1Clone = s1.clone();

允许克隆方法将学生作为返回类型。你应该这样做以避免施放\@Amir,对不起,我没有得到你的代码。正如我已经提到的,我已经实现了克隆,现在我必须实现学生的复制构造函数,我已经编写了复制构造函数,并使用它,新学生(org)现在,老师呢?你可以分享我的代码剪报,以及我如何使用它,这意味着我将称之为新教师(org)……我不知道know@AmitYadav看一看,看看这是否有助于你理解AmirBawab在说什么。