您的位置:首页 > 编程语言 > Java开发

对象克隆技术

2013-07-18 11:02 351 查看
package org.lxh.demo11.demo;

class Person implements Cloneable{
private String name = null;
public Person(String name){
this.name = name;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}

public Object clone() throws CloneNotSupportedException{
return super.clone();
}
public String toString(){
return "name: "+this.getName();
}

}

public class Demo05 {
public static void main(String args[]) throws CloneNotSupportedException {
Person per1 = new Person("zhangsan");
Person per2 = (Person)per1.clone();
((Person) per2).setName("lisi");s
System.out.println("former:"+per1);
System.out.println("later :"+per2);
}
}

此字段最重要的语句:1、public Object clone() throws CloneNotSupportedException;

2、public static void main(String args[]) throws CloneNotSupportedException {
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java