美文网首页
java 克隆一个副本

java 克隆一个副本

作者: fdsun | 来源:发表于2020-11-23 09:36 被阅读0次
    • 依赖
    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.75</version>
    </dependency>
    
    • copy
        /**
         * 克隆一个副本
         */
        public XXX copy() {
            String json = JSON.toJSONString(this);
            return JSON.parseObject(json, XXX.class);
        }
    
    • example
    import com.alibaba.fastjson.JSON;
    import lombok.Data;
    
    import java.util.Date;
    
    /**
     * @author :zxq
     * @date :Created in 2020/11/23 9:25
     */
    @Data
    public class Book {
        private String name;
        private Date date;
        private Author author;
    
        /**
         * 克隆一个副本
         */
        public Book copy() {
            String json = JSON.toJSONString(this);
            return JSON.parseObject(json, Book.class);
        }
    
        public static void main(String[] args) {
            Book book = new Book();
            book.setName("龙城");
            book.setDate(new Date());
            Author author = new Author("方想", 20);
            book.setAuthor(author);
    
            Book copy = book.copy();
            copy.setName("龙城1");
            Author author1 = new Author("方想1,", 21);
            copy.setAuthor(author1);
    
            System.out.println(book);
            System.out.println(copy);
        }
    }
    
    @Data
    class Author {
        private String name;
        private Integer age;
    
        public Author() {
        }
    
        public Author(String name, Integer age) {
            this.name = name;
            this.age = age;
        }
    }
    
    

    相关文章

      网友评论

          本文标题:java 克隆一个副本

          本文链接:https://www.haomeiwen.com/subject/gnjmiktx.html