当前位置: 首页 > news >正文

Java中Cloneable 和 clone()的总结和使用

一.Cloneable 的用途

Cloneable和Serializable一样都是标记型接口,它们内部都没有方法和属性,implements Cloneable表示该对象能被克隆,能使用Object.clone()方法。如果没有implements Cloneable的类调用Object.clone()方法就会抛出CloneNotSupportedException。

二.克隆的分类

(1)浅克隆(shallow clone),浅拷贝是指拷贝对象时仅仅拷贝对象本身和对象中的基本变量,而不拷贝对象包含的引用指向的对象。
(2)深克隆(deep clone),深拷贝不仅拷贝对象本身,而且拷贝对象包含的引用指向的所有对象。

举例区别一下:对象A1中包含对B1的引用,B1中包含对C1的引用。浅拷贝A1得到A2,A2中依然包含对B1的引用,B1中依然包含对C1的引用。深拷贝则是对浅拷贝的递归,深拷贝A1得到A2,A2中包含对B2(B1的copy)的引用,B2中包含对C2(C1的copy)的引用。

三.克隆的举例

要让一个对象进行克隆,其实就是两个步骤:
1. 让该类实现java.lang.Cloneable接口;
2. 重写(override)Object类的clone()方法。

public class Wife implements Cloneable {  
    private int id;  
    private String name;  

    public int getId() {  
        return id;  
    }  

    public void setId(int id) {  
        this.id = id;  
    }  

    public String getName() {  
        return name;  
    }  

    public void setName(String name) {  
        this.name = name;  
    }  

    public Wife(int id,String name) {  
        this.id = id;  
        this.name = name;  
    }  

    @Override  
    public int hashCode() {//myeclipse自动生成的  
        final int prime = 31;  
        int result = 1;  
        result = prime * result + id;  
        result = prime * result + ((name == null) ? 0 : name.hashCode());  
        return result;  
    }  

    @Override  
    public boolean equals(Object obj) {//myeclipse自动生成的  
        if (this == obj)  
            return true;  
        if (obj == null)  
            return false;  
        if (getClass() != obj.getClass())  
            return false;  
        Wife other = (Wife) obj;  
        if (id != other.id)  
            return false;  
        if (name == null) {  
            if (other.name != null)  
                return false;  
        } else if (!name.equals(other.name))  
            return false;  
        return true;  
    }  

    @Override  
    public Object clone() throws CloneNotSupportedException {  
        return super.clone();  
    }  

    /** 
     * @param args 
     * @throws CloneNotSupportedException  
     */  
    public static void main(String[] args) throws CloneNotSupportedException {  
        Wife wife = new Wife(1,"wang");  
        Wife wife2 = null;  
        wife2 = (Wife) wife.clone();  
        System.out.println("class same="+(wife.getClass()==wife2.getClass()));//true  
        System.out.println("object same="+(wife==wife2));//false  
        System.out.println("object equals="+(wife.equals(wife2)));//true  
    }  
}  

四.浅克隆的举例

public class Husband implements Cloneable {  
    private int id;  
    private Wife wife;  

    public Wife getWife() {  
        return wife;  
    }  

    public void setWife(Wife wife) {  
        this.wife = wife;  
    }  

    public int getId() {  
        return id;  
    }  

    public void setId(int id) {  
        this.id = id;  
    }  

    public Husband(int id) {  
        this.id = id;  
    }  

    @Override  
    public int hashCode() {//myeclipse自动生成的  
        final int prime = 31;  
        int result = 1;  
        result = prime * result + id;  
        return result;  
    }  

    @Override  
    protected Object clone() throws CloneNotSupportedException {  
        return super.clone();  
    }  

    @Override  
    public boolean equals(Object obj) {//myeclipse自动生成的  
        if (this == obj)  
            return true;  
        if (obj == null)  
            return false;  
        if (getClass() != obj.getClass())  
            return false;  
        Husband other = (Husband) obj;  
        if (id != other.id)  
            return false;  
        return true;  
    }  

    /** 
     * @param args 
     * @throws CloneNotSupportedException  
     */  
    public static void main(String[] args) throws CloneNotSupportedException {  
        Wife wife = new Wife(1,"jin");  
        Husband husband = new Husband(1);  
        Husband husband2 = null;  
        husband.setWife(wife);  
        husband2 = (Husband) husband.clone();  
        System.out.println("husband class same="+(husband.getClass()==husband2.getClass()));//true  
        System.out.println("husband object same="+(husband==husband2));//false  
        System.out.println("husband object equals="+(husband.equals(husband)));//true  
        System.out.println("wife class same="+(husband.getWife().getClass()==husband2.getWife().getClass()));//true  
        System.out.println("wife object same="+(husband.getWife()==husband2.getWife()));//true  
        System.out.println("wife object equals="+(husband.getWife().equals(husband.getWife())));//true  
    }  
}  

五.深克隆的举例

如果要深克隆,需要重写(override)Object类的clone()方法,并且在方法内部调用持有对象的clone()方法;注意如下代码的clone()方法

public class Husband implements Cloneable {  
    private int id;  
    private Wife wife;  

    public Wife getWife() {  
        return wife;  
    }  

    public void setWife(Wife wife) {  
        this.wife = wife;  
    }  

    public int getId() {  
        return id;  
    }  

    public void setId(int id) {  
        this.id = id;  
    }  

    public Husband(int id) {  
        this.id = id;  
    }  

    @Override  
    public int hashCode() {//myeclipse自动生成的  
        final int prime = 31;  
        int result = 1;  
        result = prime * result + id;  
        return result;  
    }  

    @Override  
    protected Object clone() throws CloneNotSupportedException {  
        Husband husband = (Husband) super.clone();  
        husband.wife = (Wife) husband.getWife().clone();  
        return husband;  
    }  

    @Override  
    public boolean equals(Object obj) {//myeclipse自动生成的  
        if (this == obj)  
            return true;  
        if (obj == null)  
            return false;  
        if (getClass() != obj.getClass())  
            return false;  
        Husband other = (Husband) obj;  
        if (id != other.id)  
            return false;  
        return true;  
    }  

    /** 
     * @param args 
     * @throws CloneNotSupportedException  
     */  
    public static void main(String[] args) throws CloneNotSupportedException {  
        Wife wife = new Wife(1,"jin");  
        Husband husband = new Husband(1);  
        Husband husband2 = null;  
        husband.setWife(wife);  
        husband2 = (Husband) husband.clone();  
        System.out.println("husband class same="+(husband.getClass()==husband2.getClass()));//true  
        System.out.println("husband object same="+(husband==husband2));//false  
        System.out.println("husband object equals="+(husband.equals(husband)));//true  
        System.out.println("wife class same="+(husband.getWife().getClass()==husband2.getWife().getClass()));//true  
        System.out.println("wife object same="+(husband.getWife()==husband2.getWife()));//false  
        System.out.println("wife object equals="+(husband.getWife().equals(husband.getWife())));//true  
    }  
}  

但是也有不足之处,如果Husband内有N个对象属性,突然改变了类的结构,还要重新修改clone()方法。解决办法:可以使用Serializable运用反序列化手段,调用java.io.ObjectInputStream对象的 readObject()方法。

相关文章:

  • Java并发编程之ConcurrentHashMap原理分析
  • Could not calculate build plan: Plugin org.apache.maven.plugins:maven-resources-plugin:2.5 or one of
  • 创建表--自动编号字段且自增
  • No plugin found for prefix 'jetty' in the current project and in the plugin groups [org.apache.mave
  • 关于static函数在类中的定义和使用
  • IDEA下使用Jetty进行Debug模式调试
  • SQL SERVER普通用户需要什么权限才能执行sp_configure命令
  • Ajax异步方式实现登录与参数的校验
  • xcode 不显示占用内存
  • TCP的三次握手(建立连接)和四次挥手(关闭连接)详解
  • explode 把字符串打散为数组
  • 面试精选逻辑推理题总结
  • 并发控制中的乐观锁与悲观锁
  • LinkedList基本用法
  • 数据库的隔离级别与事务的特性
  •  D - 粉碎叛乱F - 其他起义
  • ES6, React, Redux, Webpack写的一个爬 GitHub 的网页
  • HTML5新特性总结
  • leetcode98. Validate Binary Search Tree
  • Median of Two Sorted Arrays
  • socket.io+express实现聊天室的思考(三)
  • Spring技术内幕笔记(2):Spring MVC 与 Web
  • Three.js 再探 - 写一个跳一跳极简版游戏
  • 记一次用 NodeJs 实现模拟登录的思路
  • 小程序 setData 学问多
  • 用Visual Studio开发以太坊智能合约
  • 移动端高清、多屏适配方案
  • ​LeetCode解法汇总518. 零钱兑换 II
  • ​猴子吃桃问题:每天都吃了前一天剩下的一半多一个。
  • ​渐进式Web应用PWA的未来
  • ​一文看懂数据清洗:缺失值、异常值和重复值的处理
  • # Panda3d 碰撞检测系统介绍
  • # 数据结构
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • #mysql 8.0 踩坑日记
  • #pragma once与条件编译
  • (12)Hive调优——count distinct去重优化
  • (16)UiBot:智能化软件机器人(以头歌抓取课程数据为例)
  • (Java岗)秋招打卡!一本学历拿下美团、阿里、快手、米哈游offer
  • (二十四)Flask之flask-session组件
  • (附源码)计算机毕业设计SSM在线影视购票系统
  • (接口封装)
  • (四) Graphivz 颜色选择
  • (原)记一次CentOS7 磁盘空间大小异常的解决过程
  • (转载)深入super,看Python如何解决钻石继承难题
  • .NET “底层”异步编程模式——异步编程模型(Asynchronous Programming Model,APM)...
  • .Net 4.0并行库实用性演练
  • .net core 客户端缓存、服务器端响应缓存、服务器内存缓存
  • .NET gRPC 和RESTful简单对比
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .Net转Java自学之路—SpringMVC框架篇六(异常处理)
  • /bin、/sbin、/usr/bin、/usr/sbin
  • /etc/shadow字段详解
  • @cacheable 是否缓存成功_让我们来学习学习SpringCache分布式缓存,为什么用?
  • [ element-ui:table ] 设置table中某些行数据禁止被选中,通过selectable 定义方法解决