java中的clone

來源:互聯網
上載者:User

標籤:

 
TestCloneBean b = new TestCloneBean();        b.setIntegers(Lists.newArrayList(1));        String s = JsonUtils.getObjectMapperInstance().writeValueAsString(b);        TestCloneBean a = JsonUtils.getObjectMapperInstance().readValue(s, TestCloneBean.class);
 

 

1.先說一句

不去覆蓋clone方法,不去調用clone方法,除非真的有必要。 

2.java中clone兩個概念淺複製 copy的是引用深複製 copy的是執行個體,開闢新的堆空間 java中的clone方法實現的是淺複製,一個類可被淺複製需實現Cloneable(此介面只是說明此類允許clone,它改變的是超類中受保護的clone方法的行為),如effective java中的第11條所說這是介面的一種極端非典型的用法,不值得效仿。3.淺複製淺複製看上去只要實現了Cloneable介面 且@Override clone方法為public後就可以了,但是實際應用了,確定你要的是淺clone嗎?若是一個對象沒有實現Cloneable介面,也可以很簡單的使用反射實現對象的淺複製:非嚴謹的code如下:
    public static <T> T simpleClone(T obj) throws IllegalAccessException, InstantiationException {        Class<T> c = (Class<T>) obj.getClass();        T cloneC = c.newInstance();        Field[] fields = c.getDeclaredFields();        if (fields != null && fields.length > 0) {            for (Field field : fields) {                field.setAccessible(true);                Object value = field.get(obj);                field.set(cloneC, value);            }        }        return cloneC;    }

當然有很多工具類了:比如,spring的BeanUtils.copyProperties, apache的BeanUtils.copyProperties,cglib或者spring-cglib的BeanCopier 

4.深複製既然實際應用中更希望使用的是深複製,那麼如何?呢1> bean 自己實現Cloneable介面,靠譜的實現clone方法非嚴謹code如下:
public class TestCloneBean implements Cloneable {       private List<Integer> integers;     public List<Integer> getIntegers() {        return integers;    }     public void setIntegers(List<Integer> integers) {        this.integers = integers;    }     @Override    public TestCloneBean clone() {        try {            TestCloneBean t = (TestCloneBean) super.clone();            t.setIntegers(Lists.<Integer> newArrayList());            if (CollectionUtils.isNotEmpty(integers)) {                for (Integer i : integers) {                    t.getIntegers().add(i);                }            }            return t;         } catch (CloneNotSupportedException e) {            throw new RuntimeException(e);        }    }     @Override    public String toString() {        return ToStringBuilder.reflectionToString(this);    }}

 

 2>第一種方法你會這麼做嗎?不會。那麼只能另外一種方式:序列化啊!非嚴謹code如下:
public class JsonUtils {    private static ObjectMapper objectMapper = new ObjectMapper();     static {        objectMapper.configure(JsonParser.Feature.ALLOW_COMMENTS, true);        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);        objectMapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);        objectMapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS, true);        objectMapper.configure(JsonParser.Feature.INTERN_FIELD_NAMES, true);        objectMapper.configure(JsonParser.Feature.CANONICALIZE_FIELD_NAMES, true);        objectMapper.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);        objectMapper.setSerializationInclusion(Inclusion.NON_NULL);    }     public static ObjectMapper getObjectMapperInstance() {        return objectMapper;    } } 

 

testCode:
TestCloneBean b = new TestCloneBean();        b.setIntegers(Lists.newArrayList(1));        String s = JsonUtils.getObjectMapperInstance().writeValueAsString(b);        TestCloneBean a = JsonUtils.getObjectMapperInstance().readValue(s, TestCloneBean.class);

 

 

java中的clone

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.