20220901Object克隆

Java Object类中clone()方法的使用(浅克隆,深克隆)

  • 浅克隆
/**
 * 浅克隆,不可隆此对象的引用对象
 */
public class Test09013Object {
    public static void main(String[] args) throws CloneNotSupportedException {

        Boy boy =new Boy("小明");
        Girl girl =new Girl("mm");
        boy.setGirl(girl);
        System.out.println(boy);

        Boy boy2= (Boy) boy.clone();
        System.out.println(boy2);


        boy2.getGirl().name="dmm";

        System.out.println(boy2);
        System.out.println(boy);


    }
}


/**
 * 需要继承Cloneable并实现clone()方法,才可以使用clone()方法
 */
class Boy implements Cloneable{
    String name;
    Girl girl;

    public Boy(String name) {
        this.name = name;
    }

    public Girl getGirl() {
        return girl;
    }

    public void setGirl(Girl girl) {
        this.girl = girl;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "name='" + name + '\'' +
                ", girl=" + girl +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
  • 浅克隆结果
    在这里插入图片描述
  • 深克隆
/**
 * 深克隆,能可隆此对象的引用对象
 */
public class Test09013Object2 {
    public static void main(String[] args) throws CloneNotSupportedException {

        Boy2 boy =new Boy2("小明");
        Girl2 girl =new Girl2("mm");
        boy.setGirl(girl);
        System.out.println(boy);

        Boy2 boy2= (Boy2) boy.clone();
        System.out.println(boy2);


        boy2.getGirl().name="dmm";

        System.out.println(boy2);
        System.out.println(boy);


    }
}


/**
 * 需要继承Cloneable接口并实现clone()方法,才可以使用clone()方法
 */
class Boy2 implements Cloneable{
    String name;
    Girl2 girl;

    public Boy2(String name) {
        this.name = name;
    }

    public Girl2 getGirl() {
        return girl;
    }

    public void setGirl(Girl2 girl) {
        this.girl = girl;
    }

    @Override
    public String toString() {
        return "Boy{" +
                "name='" + name + '\'' +
                ", girl=" + girl +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        Boy2 boy= (Boy2) super.clone();
        Girl2 girl2= (Girl2) girl.clone();
        boy.setGirl(girl2);
        return boy;
    }
}

/**
 * 继承Cloneable接口并实现clone()方法
 */
class Girl2 implements Cloneable{
    String name;

    public Girl2(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Girl{" +
                "name='" + name + '\'' +
                '}';
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
  • 深克隆结果
    在这里插入图片描述
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值