完成对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);
}
}