12

完成对FirstLevel对象的深度克隆。
class FirstLevel implements Cloneable {
int firstIntValue;
double firstDoubleValue;
SecondLevel second;

public FirstLevel(int firstIntValue, double firstDoubleValue,
SecondLevel second) {
this.firstIntValue = firstIntValue;
this.firstDoubleValue = firstDoubleValue;
this.second = second;
}
@Override
protected Object clone() throws CloneNotSupportedException {
//利用Object的Clone方法,
FirstLevel first = (FirstLevel) super.clone();

//修改 first对象中引用类型成员变量,比如 second 成员变量引用,指向被复制的那个FirstLevel对象的second对象的拷贝,

return first;
}

public void testProtected() throws CloneNotSupportedException {
//跨包子类访问父类 ,访问FirstLevel自己从Object继承下来的Clone方法
clone(); //alt + enter

FirstLevel firstLevel = new FirstLevel(10, 20);
firstLevel.clone();
}
}

class SecondLevel implements Cloneable{
int secondIntValue;
double secondDoubleValue;
ThirdLevel third;

public SecondLevel(int secondIntValue, double secondDoubleValue,
ThirdLevel third) {
this.secondIntValue = secondIntValue;
this.secondDoubleValue = secondDoubleValue;
this.third = third;
}

@Override
protected Object clone() throws CloneNotSupportedException {
SecondLevel clone = (SecondLevel) super.clone();

//修改SecondLevel这个对象中,引用类型的成员变量的值,让他指向,复制之后的成员变量所指向对象地址
return clone;
}
}

class ThirdLevel implements Cloneable{
int thirdIntValue;
double thirdDouleValue;

public ThirdLevel(int thirdIntValue, double thirdDouleValue) {
this.thirdIntValue = thirdIntValue;
this.thirdDouleValue = thirdDouleValue;
}
}

package D12;

import java.lang.*;

class FirstLevel implements Cloneable {
    int firstIntValue;
    double firstDoubleValue;
    SecondLevel second;

    public FirstLevel(int firstIntValue, double firstDoubleValue,
                      SecondLevel second) {
        this.firstIntValue = firstIntValue;
        this.firstDoubleValue = firstDoubleValue;
        this.second = second;
    }
    @Override
    public Object clone() throws CloneNotSupportedException {
        //利用Object的Clone方法
        FirstLevel first = (FirstLevel) super.clone();
        //修改 first对象中引用类型成员变量,比如 second 成员变量引用,指向被复制的那个FirstLevel对象的second对象的拷贝,
        SecondLevel secondLevel = (SecondLevel) second.clone();
        first.second = secondLevel;
        
        return first;
    }

    public String toString(){
        return "FirstLevel{" + "firstIntValue:" + firstIntValue + " firstDoubleValue:" + firstDoubleValue + " second:" + second;
    }
}

class SecondLevel implements Cloneable{
    int secondIntValue;
    double secondDoubleValue;
    ThirdLevel third;

    public SecondLevel(int secondIntValue, double secondDoubleValue,
                       ThirdLevel third) {
        this.secondIntValue = secondIntValue;
        this.secondDoubleValue = secondDoubleValue;
        this.third = third;
    }

    @Override
    public Object clone() throws CloneNotSupportedException {
        SecondLevel second = (SecondLevel) super.clone();
        //修改SecondLevel这个对象中,引用类型的成员变量的值,让他指向,复制之后的成员变量所指向对象地址
        ThirdLevel thirdLevel = (ThirdLevel) third.clone();
        second.third = thirdLevel;
       
        return second;
    }

    public String toString(){
        return "SecondLevel{" + "secondIntValue:" + secondIntValue + " secondDoubleValue:" + secondDoubleValue + " third:" + third;
    }
}

class ThirdLevel implements Cloneable{
    int thirdIntValue;
    double thirdDouleValue;

    public ThirdLevel(int thirdIntValue, double thirdDouleValue) {
        this.thirdIntValue = thirdIntValue;
        this.thirdDouleValue = thirdDouleValue;
    }

    public Object clone() throws CloneNotSupportedException {
        //成员变量都是基本类型,所以直接调用Object的clone即可
        return super.clone();
    }

    public String toString(){
        return "ThirdLevel{thirdIntValue:" + thirdIntValue + " thirdDouleValue:" + thirdDouleValue;
    }
}
public class Q1 {
    public static void main(String[] args) throws CloneNotSupportedException{
        ThirdLevel thirdLevel = new ThirdLevel(10,20.0);
        SecondLevel secondLevel = new SecondLevel(5,10.0,thirdLevel);
        FirstLevel firstLevel = new FirstLevel(3, 5.0,secondLevel);
        System.out.println("原对象:" + firstLevel);
        FirstLevel firstClone = (FirstLevel)firstLevel.clone();
        System.out.println("克隆对象:" + firstClone);

        //修改原对象
        secondLevel.secondIntValue = 1000;
        thirdLevel.thirdIntValue = 100;
        System.out.println("修改后原对象:" + firstLevel);
        System.out.println("修改后克隆对象:" + firstClone);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值