java 实现对象的克隆

java对一个对象实例实现克隆很简单的.但是有些问题需要注意,下面通过具体的例子简单讲解.

/*能够被克隆的类要实现Cloneable 接口*/
public class Sheep
    implements Cloneable {
    private String name;
    public void setName(String arg) {
        name = arg;
    }

 

    public String getName() {
        return name;
    }

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

public class MainClass()  {
    public static void main(String[] args) throws CloneNotSupportedException {
        /* 得到一个Sheep的实例*/
        Sheep first = new Sheep();
        first.setName("我是第一只羊");
        /* 通过克隆得到另外一个Sheep的实例*/
        Sheep second = (Sheep)first.clone();
        second.setName("我是另外一只羊");
        System.out.println("addr of the first : " + first);
        System.out.println("addr of the second : " + second);
       
        System.out.println("the name of the first : " + first.getName());
        System.out.println("the name of the second : " + second.getName());
}

执行结果:
 addr of the first : common.base.clone.Sheep@192d342
addr of the second : common.base.clone.Sheep@6b97fd
the name of the first : 我是第一只羊
the name of the second : 我是另外一只羊
Java中,实现对象克隆的方法可分为浅拷贝和深拷贝,下面为你详细介绍: ### 浅拷贝 默认情况下,`Object` 类中提供的 `clone()` 方法执行浅拷贝,即不支持引用类型的成员变量的复制。使用该方法时,需要让类实现 `Cloneable` 接口并重写 `clone()` 方法。示例代码如下: ```java class EsbMongoDayServiceDataDo implements Cloneable { // 类的成员变量等定义 @Override public EsbMongoDayServiceDataDo clone() { EsbMongoDayServiceDataDo dayService = null; try { dayService = (EsbMongoDayServiceDataDo) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); } return dayService; } } ``` 此代码通过重写 `clone()` 方法调用 `super.clone()` 实现浅拷贝,若类未实现 `Cloneable` 接口,会抛出 `CloneNotSupportedException` 异常 [^3][^5]。 ### 深拷贝 若需要进行深度拷贝,即复制包含引用类型字段所引用对象,则需要进一步处理,可通过递归地对所有引用类型字段进行克隆操作或使用序列化的方式来实现 [^3]。 #### 递归克隆引用类型字段 需要在类中递归地对引用类型的成员变量进行克隆操作。示例代码如下: ```java class Address implements Cloneable { private String city; public Address(String city) { this.city = city; } @Override public Address clone() { try { return (Address) super.clone(); } catch (CloneNotSupportedException e) { e.printStackTrace(); return null; } } } class Person implements Cloneable { private String name; private Address address; public Person(String name, Address address) { this.name = name; this.address = address; } @Override public Person clone() { try { Person cloned = (Person) super.clone(); cloned.address = address.clone(); return cloned; } catch (CloneNotSupportedException e) { e.printStackTrace(); return null; } } } ``` 上述代码中,`Person` 类包含引用类型的成员变量 `Address`,在 `Person` 的 `clone()` 方法中,除了调用 `super.clone()` 克隆自身,还对 `address` 进行了克隆 [^3]。 #### 使用序列化实现 可以将对象序列化为字节流,再从字节流反序列化为新对象,以此实现深度克隆。示例代码如下: ```java import java.io.*; class DeepCopyObject implements Serializable { private String data; public DeepCopyObject(String data) { this.data = data; } public DeepCopyObject deepClone() { try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return (DeepCopyObject) ois.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); return null; } } } ``` 该代码中,`DeepCopyObject` 类实现了 `Serializable` 接口,`deepClone()` 方法通过序列化和反序列化实现对象的深度克隆 [^2]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值