有个问题一直迷惑我,既然对象可以new一个出来,为什麽要有Clone方法,他比new出来的对象有什麽优势吗?或者说什麽情况下用clone方法?实现cloneable接口?
所以你Clone一个给我
public static List getList(){return someList;} |
如果系统中的其他地方调用了
getList().clear(); |
someList |
这种操作有时是不希望发生的,这是可以使用clone方法,在get中返回
someList |
public static List getList(){return someList.clone();} |
你当然知道这个对象的接口,只是不知道内部数据和实现。