Java Clone Object

4556 단어 자바
얕 은 복사:기본 유형의 구성원 과
대상 유형 구성원 참조
깊이 복사:기본 유형의 구성원 과
대상 유형 구성원 의 인용 대상
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 + "
Favorite:" + myFavorite + "
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

좋은 웹페이지 즐겨찾기