亚洲av成人无遮挡网站在线观看,少妇性bbb搡bbb爽爽爽,亚洲av日韩精品久久久久久,兔费看少妇性l交大片免费,无码少妇一区二区三区

  免費注冊 查看新帖 |

Chinaunix

  平臺 論壇 博客 文庫
最近訪問板塊 發(fā)新帖
查看: 1361 | 回復(fù): 0
打印 上一主題 下一主題

java中的clone [復(fù)制鏈接]

論壇徽章:
1
數(shù)據(jù)庫技術(shù)版塊每日發(fā)帖之星
日期:2015-06-12 22:20:00
跳轉(zhuǎn)到指定樓層
1 [收藏(0)] [報告]
發(fā)表于 2015-06-11 09:59 |只看該作者 |倒序瀏覽
1.先說一句
不去覆蓋clone方法,不去調(diào)用clone方法,除非真的有必要。

2.java中clone兩個概念
淺克隆 copy的是引用
深克隆 copy的是實例,開辟新的堆空間

java中的clone方法實現(xiàn)的是淺克隆,一個類可被淺克隆需實現(xiàn)Cloneable(此接口只是說明此類允許clone,它改變的是超類中受保護的clone方法的行為),如effective java中的第11條所說這是接口的一種極端非典型的用法,不值得效仿。
3.淺克隆
淺克隆看上去只要實現(xiàn)了Cloneable接口 且@Override clone方法為public后就可以了,但是實際應(yīng)用了,確定你要的是淺clone么?
若是一個對象沒有實現(xiàn)Cloneable接口,也可以很簡單的使用反射實現(xiàn)對象的淺復(fù)制:
非嚴謹?shù)腸ode如下:
  1. public static <T> T simpleClone(T obj) throws IllegalAccessException, InstantiationException {
  2.         Class<T> c = (Class<T>) obj.getClass();
  3.         T cloneC = c.newInstance();
  4.         Field[] fields = c.getDeclaredFields();
  5.         if (fields != null && fields.length > 0) {
  6.             for (Field field : fields) {
  7.                 field.setAccessible(true);
  8.                 Object value = field.get(obj);
  9.                 field.set(cloneC, value);
  10.             }
  11.         }
  12.         return cloneC;
  13.     }
復(fù)制代碼
當(dāng)然有很多工具類了:比如,spring的BeanUtils.copyProperties, apache的BeanUtils.copyProperties,cglib或者spring-cglib的BeanCopier

4.深克隆
既然實際應(yīng)用中更希望使用的是深克隆,那么如何實現(xiàn)呢
bean 自己實現(xiàn)Cloneable接口,靠譜的實現(xiàn)clone方法
非嚴謹code如下:
  1. public class TestCloneBean implements Cloneable {
  2.    
  3.     private List<Integer> integers;

  4.     public List<Integer> getIntegers() {
  5.         return integers;
  6.     }

  7.     public void setIntegers(List<Integer> integers) {
  8.         this.integers = integers;
  9.     }

  10.     @Override
  11.     public TestCloneBean clone() {
  12.         try {
  13.             TestCloneBean t = (TestCloneBean) super.clone();
  14.             t.setIntegers(Lists.<Integer> newArrayList());
  15.             if (CollectionUtils.isNotEmpty(integers)) {
  16.                 for (Integer i : integers) {
  17.                     t.getIntegers().add(i);
  18.                 }
  19.             }
  20.             return t;

  21.         } catch (CloneNotSupportedException e) {
  22.             throw new RuntimeException(e);
  23.         }
  24.     }

  25.     @Override
  26.     public String toString() {
  27.         return ToStringBuilder.reflectionToString(this);
  28.     }
  29. }
復(fù)制代碼
第一種方法你會這么做么?不會。那么只能另外一種方式:序列化啊!
非嚴謹code如下:
  1. public class JsonUtils {
  2.     private static ObjectMapper objectMapper = new ObjectMapper();

  3.     static {
  4.         objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);
  5.         objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
  6.         objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
  7.         objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
  8.         objectMapper.configure(JsonParser.Feature.INTERN_FIELD_NAMES, true);
  9.         objectMapper.configure(JsonParser.Feature.CANONICALIZE_FIELD_NAMES, true);
  10.         objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
  11.         objectMapper.setSerializationInclusion(Inclusion.NON_NULL);
  12.     }

  13.     public static ObjectMapper getObjectMapperInstance() {
  14.         return objectMapper;
  15.     }

  16. }
復(fù)制代碼
testCode如下:
  1. TestCloneBean b = new TestCloneBean();
  2.         b.setIntegers(Lists.newArrayList(1));
  3.         String s = JsonUtils.getObjectMapperInstance().writeValueAsString(b);
  4.         TestCloneBean a = JsonUtils.getObjectMapperInstance().readValue(s, TestCloneBean.class);
復(fù)制代碼
您需要登錄后才可以回帖 登錄 | 注冊

本版積分規(guī)則 發(fā)表回復(fù)

  

北京盛拓優(yōu)訊信息技術(shù)有限公司. 版權(quán)所有 京ICP備16024965號-6 北京市公安局海淀分局網(wǎng)監(jiān)中心備案編號:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年舉報專區(qū)
中國互聯(lián)網(wǎng)協(xié)會會員  聯(lián)系我們:huangweiwei@itpub.net
感謝所有關(guān)心和支持過ChinaUnix的朋友們 轉(zhuǎn)載本站內(nèi)容請注明原作者名及出處

清除 Cookies - ChinaUnix - Archiver - WAP - TOP