Java中必需的可复制接口 [英] Mandatory cloneable interface in Java

查看:134
本文介绍了Java中必需的可复制接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中遇到一个小问题。我有一个名为Modifiable的界面。实现此接口的对象是可修改的。

I'm having a small problem in Java. I have an interface called Modifiable. Objects implementing this interface are Modifiable.

我还有一个ModifyCommand类(带有Command模式),它接收两个可修改的对象(在列表中进一步交换它们 - 这是不是我的问题,我已经设计了这个解决方案。)

I also have a ModifyCommand class (with the Command pattern) that receive two Modifiable objects (to swap them in a list further on - that's not my question, I designed that solution already).

ModifyCommand类首先制作可修改对象的克隆。从逻辑上讲,我使我的Modifiable接口扩展了Cloneable。接口然后定义一个clone()方法,它的实现类必须重新定义。

The ModifyCommand class starts by making clones of the Modifiable objects. Logically, I made my Modifiable interface extends Cloneable. The interface then defines a clone() method that its implementing classes must redefine.

然后,在ModifyCommand中,我可以这样做:firstModifiableObject.clone()。我的逻辑是实现Modifiable的类必须从Object重新定义克隆方法,因为它们将是Cloneable(这就是我想要做的)。

Then, in ModifyCommand, I can do : firstModifiableObject.clone(). My logic is that classes implementing Modifiable will have to redefine the clone method from Object, as they will be Cloneable (that's what I want to do).

事情是,当我定义类实现Modifiable并且我想覆盖clone()时,它不会让我,说明Object类中的clone()方法隐藏了一个来自Modifiable的方法。

The thing is, when I define classes implements Modifiable and I want to override clone(), it won't let me, stating that the clone() method from the Object class hides the one from Modifiable.

我该怎么办?我的印象是我做错了......

What should I do? I'm under the impression that "I'm doing it wrong"...

谢谢,

Guillaume。

Guillaume.

编辑:它认为我会忘记clone()的事情。我将a)假设传递给Modifiable对象(实现接口)的对象已经克隆或b)创建另一个调用的方法,例如copy(),它基本上会执行Modifiable对象的深层复制(或者通用解决方案可以工作......)。

Edit : it think I will forget the clone() thing. I will either a) assume that the object passed to the Modifiable object (implementing the interface) is already cloned or b) make another method called, for example, copy(), that would basically do a deep-copy of the Modifiable object (or maybe the Generic solution will work...).

推荐答案

如果您使用的是Java 1.5或更高版本,则可以获得您想要的行为并以这种方式删除:

If you're using java 1.5 or higher, you can get the behavior you want and remove casting this way:

public interface Modifiable<T extends Modifiable<T>> extends Cloneable {
    T clone();
}

public class Foo implements Modifiable<Foo> {
    public Foo clone() { //this is required
        return null; //todo: real work
    }
}

由于Foo扩展了Object,这仍然满足Object类的原始契约。由于Modifiable接口施加了额外的限制,因此无法正确编译clone()方法的代码将无法编译。作为奖励,调用代码不必转换克隆方法的结果。

Since Foo extends Object, this still satisfies the original contract of the Object class. Code that doesn't refine the clone() method correctly will not compile, because of the additional constraints imposed by the Modifiable interface. As a bonus, calling code doesn't have to cast the result of the clone method.

这篇关于Java中必需的可复制接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆