//================================================= // File Name : Clone_demo //------------------------------------------------------------------------------ // Author : Common //类名:Person__ //属性: //方法: class Person__ implements Cloneable{ //必须实现Cloneable接口 private String name = null; public Person__(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } //必须子类覆写clone方法 public Object clone() throws CloneNotSupportedException{ return super.clone(); //具体的克隆操作由父类完成 } @Override public String toString() { return "姓名:"+this.getName(); } } //主类 //Function : Clone_demo public class Clone_demo { public static void main(String[] args) throws CloneNotSupportedException { // TODO 自动生成的方法存根 Person__ p1 = new Person__("张三"); Person__ p2 = (Person__) p1.clone(); p2.setName("李四"); //克隆之后修改name的参数 System.out.println("原始对象:"+p1); System.out.println("克隆之后的对象:"+p2); } }
时间: 2024-10-18 22:26:08