#注意这里的区别实现深复制,浅复制的方式和区别,同时还可以通过对象序列化的方式实现深复制 class Protype implements Cloneable{ private int age; private String name; public Protype(String name,int age){ this.name=name; this.age=age; } //getter and setter method @Override public Object clone(){ try { Protype protype=(Protype)super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return null; } public static void main(String[] args){ Protype protype=new Protype("liurong",24); Protype protype1=(Protype)protype.clone(); protype.name="wangliu"; System.out.println(protype.name); //输出为wangliu; } }
原型设计模式
最新推荐文章于 2024-07-24 17:27:32 发布