Java 公共克隆接口

发布于 2024-11-24 07:18:13 字数 198 浏览 1 评论 0 原文

创建这样的接口并在我需要确保变量可克隆的地方使用它有什么坏处或错误吗?

public interface PublicCloneable<I> {
    public I clone();
}

SO 中的问题与 java 的 Cloneable 接口已损坏这一事实相关,我不明白为什么它不这样实现。

Is there anything bad or wrong about creating an interface like this and use it in a place i need to make sure a variable is cloneable?

public interface PublicCloneable<I> {
    public I clone();
}

The are questions in SO related on the fact that the Cloneable interface of java is broken and i don't understand why it isn't implemented like this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

失眠症患者 2024-12-01 07:18:14

你可以。创建新接口的主要问题是您只能在您创建的显式实现此接口的新类上使用此接口。 Java 库中的现有类无法实现此接口,因为您无法更改它们的代码。 (该接口不会神奇地应用于现有类型。)因此,只有当您为您期望使用的所有对象创建自定义类系列并且不使用标准库类时,它才有用。

You can. The main problem with creating a new interface is that you can only use this interface on new classes that you create, which explicitly implement this interface. Existing classes in the Java library cannot implement this interface, since you can't change their code. (The interface does not magically apply to existing types.) So it's only useful if you kind of create of create a family of custom classes for all the objects you expect to use, and don't use the standard library classes.

微暖i 2024-12-01 07:18:14

如果您想完全使用自己的 clone() 实现,那应该没问题。但是,如果您想在某个时候使用 Object.clone(),我建议

public interface PublicCloneable<I> extends Cloneable {
    public I clone();
}

在实现内部:

   public static class MyClass implements PublicCloneable<MyClass> {
     public MyClass clone() {
        try {
            return (MyClass)super.clone(); // Or do whatever you need here
        } catch (CloneNotSupportedException e) {
            // Always supported
        }
   }

我不确定它是否可以编译,但我尝试过,看起来没问题。

当然,里程可能会有所不同。

If you want to go with entirely your own implementation of clone() it should be OK. However, if you want to use Object.clone() at some point, I'd recommend

public interface PublicCloneable<I> extends Cloneable {
    public I clone();
}

and inside implementation:

   public static class MyClass implements PublicCloneable<MyClass> {
     public MyClass clone() {
        try {
            return (MyClass)super.clone(); // Or do whatever you need here
        } catch (CloneNotSupportedException e) {
            // Always supported
        }
   }

I was not sure if it compiles, but I tried and it seems ok.

Mileage can vary, of course.

小兔几 2024-12-01 07:18:14

这很好,但您必须在方法内提供您自己的克隆逻辑。

java.lang.Cloneable 的思想是将一个类标记为可克隆,并由 JVM 处理克隆逻辑。您不使用 Object.clone() 提供逐字段克隆

您可以从 这个答案

This is fine, but you will have to provide your own cloning logic inside the method.

The idea of java.lang.Cloneable is to mark a class as cloneable and the cloning logic to be handled by the JVM. You don't provide field-by-field cloning with Object.clone()

You can pick another cloning mechanism (or use your interface in combination with another one) from the ones suggested in this answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文