Cloneable


1.Cloneable顾名思义就是复制,克隆。接下来我们将要使用Cloneable进行Java中克隆。(浅克隆)

实现一个简单的克隆如下:

import java.util.Arrays;

public class Test2 {
    public static void main(String[] args)throws CloneNotSupportedException {
        Person person1=new Person(18,"niechentao");
        Person person2=(Person)person1.clone();
        System.out.println(person1);
        System.out.println(person2);;
    }
}
package demo2;
public class Person implements Cloneable{
    public int age;
    public String name;
    public Person(int age,String name){
        this.age=age;
        this.name=name;
    }
    public String toString(){
        return "["+this.name+":"+this.age+"]";
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
      return super.clone();
    }
}
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:D:\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=52174:D:\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\niech\IdeaProjects\untitled15\out\production\untitled15 demo2.Test2
[niechentao:18]
[niechentao:18]

Process finished with exit code 0

要注意几步:

1.要在person中重写clone方法。

2.要注意在person中接入Cloneable接口。

3.要注意异常的排除。

4.记得要向下转型。

2.如何实现深度克隆

import java.util.Arrays;

public class Test2 {
    public static void main(String[] args)throws CloneNotSupportedException {
        Person person1=new Person(18,"niechentao");
        Person person2=(Person)person1.clone();
       person1.m.money=10;
        System.out.println(person1.m.money);
        System.out.println(person2.m.money);

    }
}
class Money{
    public int money;
}
public class Person implements Cloneable{
    public int age;
    public String name;
    public Money m=new Money();
    public Person(int age,String name){
        this.age=age;
        this.name=name;
    }
    public String toString(){
        return "["+this.name+":"+this.age+"]";
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
      return super.clone();
    }
}
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:D:\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=52374:D:\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\niech\IdeaProjects\untitled15\out\production\untitled15 demo2.Test2
10
10

Process finished with exit code 0

如上式代码可见:person1.m.money改变person2.m.money与其发生一样的改变。我们希望person2.m.money不随着person1.m.money改变而改变。

1.我们需要money也实现clone方法。

2.在person中的clone方法中先要克隆person类中的数,再通过引用克隆money的类。

package demo2;
class Money implements Cloneable{
    public int m=99;
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public class Person implements Cloneable{
    public int age;
    public String name;
    public Money money=new Money();
    public Person(int age,String name){
        this.age=age;
        this.name=name;
    }
    public String toString(){
        return "["+this.name+":"+this.age+"]";
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
       Person tmp=(Person)super.clone();
      tmp.money=(Money)this.money.clone();
      return tmp;
    }
}
package demo2;

import java.util.Arrays;

public class Test2 {
    public static void main(String[] args)throws CloneNotSupportedException {
        Person person1=new Person(18,"niechentao");
        Person person2=(Person)person1.clone();
        person1.money.m=0;
        System.out.println(person1.money.m);
        System.out.println(person2.money.m);
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值