从 java.lang.Object 访问 clone()
这是我无法理解的事情。
在java.lang.Object
中,clone()
是用protected
修饰符定义的。根据定义,可以通过其自己的类定义中的名称、从其派生的任何类中的名称以及同一包中任何类的定义中的名称来访问它。
这里的 Sample
类位于另一个包中,显然它无法从 Object
类访问 clone()
。但由于 Sample
隐式派生自 Object
,为什么它无法访问它呢?该定义并没有说它必须满足这两个条件(在同一包内并且也是一个子类)。
public class Sample {
public Object foo() throws CloneNotSupportedException {
...
return someObject.clone();
}
}
Here is something that I cannot understand.
In java.lang.Object
the clone()
is defined with protected
modifier. By definition than it can be accessed by name inside its own class definition, by name inside any class derived from it, and by name in the definition of any class in the same package.
Here the Sample
class is in another package, and obviously it can't access clone()
from the Object
class. But as Sample
derives implicitly from Object
, why is it not able to access it? The definition doesn't say that it HAS to satisfy both conditions (inside same package AND also to be a subclass).
public class Sample {
public Object foo() throws CloneNotSupportedException {
...
return someObject.clone();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的情况下,
clone()
方法不可见,因为您没有从子类调用它。Sample
派生自Object
,因此它可以访问自己的clone()
方法,但不能访问其他对象的方法。对象
clone()
的设计有几个错误。因此,使用它不是一个好的做法 - 很难做到正确:clone()
使其公开,它将仍然失败,因为每个类都必须实现Cloneable
Cloneable
,但是,没有定义任何方法,因此对象的用户无法将其引用为可克隆
并期望一个克隆方法。检查 这个问题作为替代方案。
In your case, the
clone()
method is not visible because you are not calling it from a subclass.Sample
derives fromObject
, so it can access its ownclone()
method, but not that of other objects.Object
clone()
was designed with several mistakes. So it is not a good practice to use it - it is very hard to get it right:clone()
making it public, it will still fail, because each class has to implementCloneable
Cloneable
, however, does not define any methods, so the users of the objects can't refer to it asCloneable
and expect a clone method.super.clone()
for the default cloning mechanism to workCheck this question for alternatives.
对我有用: http://ideone.com/eST8Y
编译没有错误。 它仍然抛出运行时
CloneNotSupportedException
因为Main
没有实现可克隆
。@Bozho 的答案确实是正确的答案。 请勿使用
Object.clone()
。请参阅有效的 Java,第 10 项:覆盖
明智地克隆
(后续版本中的第11条)。Works for me: http://ideone.com/eST8Y
This compiles without error. It still throws a runtime
CloneNotSupportedException
becauseMain
does not implementCloneable
.@Bozho's answer is really the right answer here, though. Just don't use
Object.clone()
.See Effective Java, Item 10: Override
clone
judiciously (Item 11 in later editions).someObject
的类类型在这里很重要。对象someObject
可能不会覆盖Object
类的clone()
方法,因此 make 对类Sample
不可见。The class type of
someObject
is important here. ObjectsomeObject
might not be overriding theclone()
method ofObject
class, thus making is invisible to classSample
.“无法访问”是什么意思?你的意思是它不会编译还是你的意思是它抛出 CloneNotSupportedException。
如果您的类未实现 Cloneable 接口,
Object.clone()
将抛出CloneNotSupportedException
。What do you mean "not able to access it"? Do you mean it won't compile or do you mean it throws the CloneNotSupportedException.
Object.clone()
will throwCloneNotSupportedException
if your class doesn't implement the Cloneable interface.