JAVA深复制(深克隆)与浅复制(浅克隆)

JAVA深复制(深克隆)与浅复制(浅克隆)
1.浅复制与深复制概念
⑴浅复制(浅克隆)
被复制对象的所有变量都含有与原来的对象相同的值,而所有的对其他对象的引用仍然指向原来的对象。换言之,浅复制仅仅复制所考虑的对象,而不复制它所引用的对象。
⑵深复制(深克隆)
被复制对象的所有变量都含有与原来的对象相同的值,除去那些引用其他对象的变量。那些引用其他对象的变量将指向被复制过的新对象,而不再是原有的那些被引用的对象。换言之,深复制把要复制的对象所引用的对象都复制了一遍。
2.Java的clone()方法
⑴clone方法将对象复制了一份并返回给调用者。一般而言,clone()方法满足:
①对任何的对象x,都有x.clone() !=x//克隆对象与原对象不是同一个对象
②对任何的对象x,都有x.clone().getClass()= =x.getClass()//克隆对象与原对象的类型一样
③如果对象x的equals()方法定义恰当,那么x.clone().equals(x)应该成立。
⑵Java中对象的克隆
①为了获取对象的一份拷贝,我们可以利用Object类的clone()方法。
②在派生类中覆盖基类的clone()方法,并声明为public。
③在派生类的clone()方法中,调用super.clone()。
④在派生类中实现Cloneable接口。

public class Student implements Cloneable {
    private String name;
    private int age;
    private Professor p;

    public Student(String name, int age, Professor p) {
        super();
        this.name = name;
        this.age = age;
        this.p = p;
    }

    public Professor getP() {
        return p;
    }
    public void setP(Professor p) {
        this.p = p;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }
    //深复制
    @Override
//  public Object clone(){
        Object o = null;
//      Student s = null;
//      try {
//          s= (Student)super.clone();
//          s.p = (Professor)this.p.clone();
//      } catch (CloneNotSupportedException e) {
//          e.printStackTrace();
//      }
//      return s;
//  }
    //浅复制
    public Object clone(){
        Object o = null;
        try {
            o= (Student)super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }
    public static void main(String[] args) {
        Professor p=new Professor("王五",50); 
        Student s1 = new Student("张三", 17,p);
        Student s2 = (Student)s1.clone();
        s2.age=12;
        s2.name= "李四";
        s2.p.setName("赵六");
        s2.p.setAge(60);
        System.out.println("name=" + s1.name + "," + "age="
        + s1.age + "," + "Professor name =" + s1.p.getName()+ ","+"Professor age =" + s1.p.getAge());
        System.out.println("name=" + s2.name + "," + "age=" 
        + s2.age+ ","+ "Professor name =" + s2.p.getName()+ ","+"Professor age =" + s2.p.getAge());
    }

}
class Professor implements Cloneable {    
    private String name;    
    private int age;  
     public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    Professor(String name,int age)    
     {    
        this.name=name;    
        this.age=age;    
     }
    @Override
    protected Object clone() {
        Object o = null;
        try {
            o= (Professor)super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }  

}  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值