gpt4 book ai didi

java - 复制构造函数 v. 实现 Cloneable 接口(interface)

转载 作者:搜寻专家 更新时间:2023-10-31 08:11:19 25 4
gpt4 key购买 nike

就“最佳实践”而言,哪种方法更适合创建对象的“深拷贝”?

最佳答案

使用复制构造函数。 Cloneable 是一个直接的 API 灾难。参见 Effective Java Item 10 (Item 11 in the 2nd. ed.)。

Item 11: Override clone judiciously

The Cloneable interface was intended as a mixin interface (item 18) for objects to advertise that they permit cloning. Unfortunately, it fails to serve this purpose. Its primary flaw is that it lacks a clone method, and Object's clone method is protected. You cannot, without resorting to reflection (Item 53), invoke the clone method on an object merely because it implements Cloneable. Even a reflective invocation may fail, as there is no guarantee that the object has an accessible clone method.

关于java - 复制构造函数 v. 实现 Cloneable 接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8650457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com