如何使用 Cloneable 类型作为 Java 泛型类的参数
我有一个泛型类,需要能够克隆参数类型的对象。下面是一个非常简单的例子。编译器声称 Object 类型的 clone() 不可见。
public class GenericTest<T extends Cloneable>
{
T obj;
GenericTest(T t)
{
obj = t;
}
T getClone()
{
// "The method clone() from the type Object is not visible."
return (T) obj.clone();
}
}
我不想让调用者进行克隆,因为为了维护对象的完整性还必须发生其他事情。上面的代码只是问题的说明,没有我必须维护的与克隆对象相关的其他数据的噪音。
有没有办法解决这个问题,或者这是否是 Java 设计者考虑合理化其缺点的另一种情况?
I have a generic class that needs to be able to clone objects of the parameter type. A very simple example is below. The compiler claims clone() from the type Object is not visible.
public class GenericTest<T extends Cloneable>
{
T obj;
GenericTest(T t)
{
obj = t;
}
T getClone()
{
// "The method clone() from the type Object is not visible."
return (T) obj.clone();
}
}
I'd prefer not to have the caller do the cloning since there are other things that have to happen to maintain the integrity of the object. The code above is just an illustration of the problem without the noise of the other data I have to maintain related to the cloned object.
Is there a way around this or is this another one of those cases where the designers of Java consider rationalizing its shortcomings the equivalent of having none?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于该方法在
Object
类上被标记为protected
,因此通常不能在任意对象上调用该方法。就我个人而言,我一开始并不认为这会是一个问题(嘿,我是Object
的子类,所以我应该能够调用它的受保护方法,对吧?),但是编译器需要知道您是目标对象的类(或其包中)的子类,以便调用受保护的方法,这两者都不适用。clone()
方法背后的想法是,支持该方法的类将重写该方法,将其声明为public
。这里保留完整功能的唯一真正的解决方案是使用反射来访问方法并绕过访问修饰符。另一种方法是编写您自己的
MyCloneable
接口,其中声明了 publicclone()
方法;如果您只传递自己的域类,这可能会起作用,但这意味着您无法在外部类(例如java.util.String
或java.util.String)上使用它。 util.ArrayList
),因为你不能强迫他们实现你的接口。根据链接问题的答案,这是一个非常可疑的设计。
Because the method is marked as
protected
on theObject
class, you cannot in general call this method on arbitrary objects. Personally I didn't think this would be a problem at first (hey, I'm a subclass ofObject
, so I should be able to call its protected methods, right?), but the compiler needs to know that you're a subclass of the target object's class (or in its package) in order to call protected methods, neither of which apply here.The idea behind the
clone()
method is that classes which supported it would override the method, declaring it aspublic
.The only real solution here that preserves full functionality is to use reflection to access the method and get around the access modifiers. An alternative would be to write your own
MyCloneable
interface which has a publicclone()
method declared on it; this might work if you'll only ever be passing your own domain classes in, but means that you couldn't use it on external classes (such asjava.util.String
orjava.util.ArrayList
) since you can't force them to implement your interface.As per answers to the linked question, this is a very dubious design.
Java 方面的一个错误。反思才是正确的出路
A mistake on Java's part. Reflection is the right way to go
我需要克隆一个包含字段的 POJO。有效的方法如下:
I needed to clone a POJO with fields in it. What did work was the following: