Java Clone Object

浅拷贝:拷贝基本类型成员和[color=red]对象类型成员的引用[/color]
深度拷:拷贝基本类型成员和[color=red]对象类型成员的引用对象[/color]
package test;

public class CloneTest
{
public static void main(String[] args)

// 浅度拷贝Shallow copy
Type type = new Type("cat");
MyObject obj1 = new MyObject(10, type);
MyObject obj2 = (MyObject) obj1.clone();
obj1.type.type = "dog";
System.out.println("******************浅度拷贝****************");
System.out.println("拷贝数据类型成员和对象类型成员的引用");
boolean isSame = obj1.type == obj2.type;
System.out.println("obj1.type == obj2.type:" + isSame);
System.out.println(obj1);
System.out.println(obj2);

// 深度拷贝Deep copy
MyObject obj = new MyObject(10, type);
MyHobby hobby1 = new MyHobby(obj, 25, "king");
MyHobby hobby2 = (MyHobby) hobby1.clone();

hobby1.myFavorite = new MyObject(1, new Type("mouse"));
System.out.println("******************深度拷******************");
System.out.println("拷贝基本类型成员和对象类型成员的引用对象");
isSame = hobby1.myFavorite == hobby2.myFavorite;
System.out.println("hobby1.myFavorite == hobby2.myFavorite:" + isSame);
System.out.println(hobby1);
System.out.println(hobby2);
}
}

class Type
{
String type;

public Type(String type)
{
this.type = type;
}

public String getType()
{
return type;
}

public void setType(String type)
{
this.type = type;
}

public String toString()
{
return this.type;
}
}
class MyObject implements Cloneable
{
private int age;

public Type type;

MyObject()
{

}

public MyObject(int age, Type type)
{
this.age = age;
this.type = type;
}

public int getAge()
{
return age;
}

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

public Type getType()
{
return type;
}

public void setType(Type type)
{
this.type = type;
Math.random();
}

public Object clone()
{
Object obj = null;
try
{
obj = super.clone();
} catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
return obj;
}

public String toString()
{
return "{name:" + this.type + ",age:" + this.age + "}";
}
}
class MyHobby implements Cloneable
{
public MyObject myFavorite;

private int myAge;

public String myName;

MyHobby()
{

}

public MyHobby(MyObject myFavorite, int myAge, String myName)
{
this.myFavorite = myFavorite;
this.myAge = myAge;
this.myName = myName;
}

public int getMyAge()
{
return myAge;
}

public void setMyAge(int myAge)
{
this.myAge = myAge;
}

public MyObject getMyFavorite()
{
return myFavorite;
}

public void setMyFavorite(MyObject myFavorite)
{
this.myFavorite = myFavorite;
}

public String getMyName()
{
return myName;
}

public void setMyName(String myName)
{
this.myName = myName;
}

@Override
protected Object clone()
{
MyHobby obj = null;
try
{
obj = (MyHobby) super.clone();
} catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
obj.myFavorite = (MyObject) myFavorite.clone();
return obj;
}

public String toString()
{
return this.myName + "\n Favorite:" + myFavorite + "\n age:" + myAge;
}
}


运行的结果:

******************浅度拷贝****************
*拷贝基本数据类型成员和对象类型成员的引用*
obj1.type == obj2.type:true
{name:dog,age:10}
{name:dog,age:10}
******************深度拷贝******************
*拷贝基本数据类型成员和对象类型成员的引用对象*
hobby1.myFavorite == hobby2.myFavorite:false
king
Favorite:{name:mouse,age:1}
age:25
king
Favorite:{name:dog,age:10}
age:25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值