对象克隆技术

class Person implements Cloneable{
	private String name=null;
	public Person(String name){
		this.name = name;
	}
	public void setName(String name){
		this.name = name;
	}
	public String getName(){
		return name;
	}
	public Object clone() throws CloneNotSupportedException{
		return super.clone();
	}
	public String toString(){
		return ("姓名:"+ this.getName());
	}
}

public class Clone {
	public static void main(String []args)throws Exception{
		Person p1 = new Person("张三");
		Person p2 = (Person)p1.clone();//克隆
		System.out.println(p1);
		System.out.println(p2);
		p2.setName("李四");
		System.out.println(p2);
	}
}

猜你喜欢

转载自blog.csdn.net/Deaful/article/details/8108487