Java 中的强制可克隆接口
我在 Java 中遇到了一个小问题。 我有一个名为“可修改”的界面。 实现此接口的对象是可修改的。
我还有一个 ModifyCommand 类(具有命令模式),它接收两个可修改对象(以进一步在列表中交换它们 - 这不是我的问题,我已经设计了该解决方案)。
ModifyCommand 类首先创建可修改对象的克隆。 从逻辑上讲,我使我的可修改接口扩展了可克隆接口。 然后,该接口定义了一个clone()方法,其实现类必须重新定义该方法。
然后,在ModifyCommand中,我可以执行以下操作:firstModifyingObject.clone()。 我的逻辑是,实现可修改的类必须重新定义对象的克隆方法,因为它们将是可克隆的(这就是我想要做的)。
问题是,当我定义类实现 Modulated 并且我想重写clone() 时,它不会让我这样做,并指出 Object 类中的clone() 方法隐藏了可修改的方法。
我应该怎么办? 我的印象是“我做错了”......
谢谢,
纪尧姆。
编辑:我想我会忘记clone()的事情。 我将 a) 假设传递给可修改对象(实现接口)的对象已经被克隆,或者 b) 调用另一个方法,例如 copy(),这基本上会执行可修改对象的深层复制(或者通用解决方案可能会起作用......)。
I'm having a small problem in Java. I have an interface called Modifiable. Objects implementing this interface are Modifiable.
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).
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.
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).
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"...
Thanks,
Guillaume.
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...).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您使用的是 java 1.5 或更高版本,您可以获得所需的行为并以这种方式删除强制转换:
由于 Foo 扩展了 Object,因此这仍然满足 Object 类的原始约定。 由于 Modificable 接口施加的额外约束,未正确细化 clone() 方法的代码将无法编译。 额外的好处是,调用代码不必强制转换克隆方法的结果。
If you're using java 1.5 or higher, you can get the behavior you want and remove casting this way:
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.
您不需要在Modabilible接口上重新定义clone方法。
检查文档: http://java.lang. sun.com/j2se/1.4.2/docs/api/java/lang/Cloneable.html
我知道你试图强迫每个人重写clone method(),但你做不到。
换句话说,您不能重写接口上的类:
clone() 方法始终与 Object.class 关联,而不是与可克隆接口关联。 您只能在另一个对象上覆盖它,而不是在接口中。
You don't need to redefine the clone method on the interface Modifiable.
Check the documentation: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Cloneable.html
I understand that you are trying to force everyone to override clone method(), but you can't do it.
In another way, you cannot override a class on a interface:
The clone() method is always associated whit the Object.class and not the cloneable interface. You just can override it on another object, not in a interface.
添加 Sean Reilly 的答案,这应该可以解决您的问题,并且类型更加安全。 它在 JDK6 上编译并运行良好:
我无法使用 Java 5 测试它,因为我没有安装它,但我想它会工作得很好。
Adding to Sean Reilly's answer, this should solve your problem, and is more type safe. It compiles and runs fine with me on JDK6:
I couldn't test it with Java 5 because I don't have it installed, but I guess it would work fine.
您定义的签名是否与对象中的签名完全相同?
这应该编译 - 将自定义代码添加到正文中。 Wikipedia 在这方面出人意料地有帮助。
Did you define the signature exactly as it is in object?
This should compile - add custom code to the body. Wikipedia was surprisingly helpful on this one.
您的克隆方法的方法签名是什么样的? 为了匹配 Clonable 接口,它必须返回一个对象。 如果您将其声明为返回可修改的对象,那么这可能就是问题所在。
What does your method signature for your clone method look like? For it to match the Clonable interface it would have to return an Object. If you're declaring it as returning a Modifiable then that could be the problem.
公共类 CloningExample 实现 Cloneable {
}
public class CloningExample implements Cloneable {
}