浅拷贝:拷贝基本类型成员和[color=red]对象类型成员的引用[/color]
深度拷:拷贝基本类型成员和[color=red]对象类型成员的引用对象[/color]
运行的结果:
******************浅度拷贝****************
*拷贝基本数据类型成员和对象类型成员的引用*
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
深度拷:拷贝基本类型成员和[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