Java对象克隆

本文深入解析对象克隆的概念,探讨浅拷贝与深拷贝的区别,以及如何在Java中实现对象的克隆,包括实现Cloneable接口和覆写clone方法的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

问题

  • 什么是克隆
  • 对象克隆的方法
  • 浅拷贝和深拷贝

解答

  • 克隆是创建对象的方式之一,通过克隆可以将原有的对象完全克隆出一个新的对象且该对象的所有属性被复制过来。在对象比较复杂的时候,克隆为一种效率高的方式。
  • 对象克隆的方法
  1. 被克隆的类要覆写Object的clone,并改为public
  2. 被克隆的类要实现Cloneable接口
    例子:

class Student  implements Cloneable{
    private String name;
    private int age;

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

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
    //此处将clone改为public类型
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}
public class CloneApp {
    public static void main(String[] args) throws CloneNotSupportedException {
        Student s1 = new Student("Tom",20);
        Student s2 = (Student)s1.clone();
        System.out.println(s2.getName());
        System.out.println(s2.getAge());
    }
}
  • 浅拷贝
    默认被克隆的对象中的属性中有其他对象的引用的时候,不会把该引用的对象也克隆,导致了只会克隆该对象,而不会克隆该对象的属性的引用。
class Score{
    private int english ;
    private int math;

    public Score(int english, int math) {
        this.english = english;
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }
}
class Student  implements Cloneable{
    private String name;
    private int age;
    private Score score;

    public void setName(String name) {
        this.name = name;
    }

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

    public Score getScore() {
        return score;
    }

    public void setScore(Score score) {
        this.score = score;
    }

    public Student(String name, int age, Score score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

}
public class CloneApp {
    public static void main(String[] args) throws CloneNotSupportedException {;
        Score score = new Score(100,80);
        Student s1 = new Student("Tom",20,score);
        Student s2 = (Student)s1.clone();
        System.out.println(s2.getName());
        System.out.println(s2.getAge());
        //s1的score的属性被修改了,其实s1和s2持有的是同一个对象。
        s2.getScore().setEnglish(50);
        s2.getScore().setMath(70);

        System.out.println("===s1的score===");
        System.out.println("english="+s1.getScore().getEnglish());
        System.out.println("math="+s1.getScore().getMath());

    }
}
  • 深拷贝
    深拷贝就是讲被克隆的对象引用的其他对象也克隆了。实现方法如下
class Score implements Cloneable{
    private int english ;
    private int math;

    public Score(int english, int math) {
        this.english = english;
        this.math = math;
    }

    public int getEnglish() {
        return english;
    }

    public void setEnglish(int english) {
        this.english = english;
    }

    public int getMath() {
        return math;
    }

    public void setMath(int math) {
        this.math = math;
    }
    @Override
    public Object clone () throws CloneNotSupportedException {
        return super.clone();
    }
}
class Student  implements Cloneable{
    private String name;
    private int age;
    private Score score;

    public void setName(String name) {
        this.name = name;
    }

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

    public Score getScore() {
        return score;
    }

    public void setScore(Score score) {
        this.score = score;
    }

    public Student(String name, int age, Score score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        Student s = (Student)super.clone();
        //把Score克隆了
        s.score = (Score) score.clone();
        return s;
    }

}
public class CloneApp {
    public static void main(String[] args) throws CloneNotSupportedException {;
        Score score = new Score(100,80);
        Student s1 = new Student("Tom",20,score);
        Student s2 = (Student)s1.clone();
        System.out.println(s2.getName());
        System.out.println(s2.getAge());
        s2.getScore().setEnglish(50);
        s2.getScore().setMath(70);

        System.out.println("===s1的score===");
        System.out.println("english="+s1.getScore().getEnglish());
        System.out.println("math="+s1.getScore().getMath());

        System.out.println("===s2的score===");
        System.out.println("english="+s2.getScore().getEnglish());
        System.out.println("math="+s2.getScore().getMath());

    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值