可克隆的行为
Java 文档说 -
类 Object 本身并不 实现Cloneable接口,所以 调用对象的克隆方法 其类是 Object 将导致 在运行时抛出异常。
这就是为什么 Object 类中的 clone 方法受到保护?是这样吗 ?
这意味着任何未实现 Cloneable 的类在调用其 Clone 方法时都会抛出 CloneNotSupported 异常。
我运行了一个测试程序
public class Test extends ABC implements Cloneable{
@Override
public Object clone() throws CloneNotSupportedException {
System.out.println("calling super.clone");
return super.clone();
}
public static void main(String[] args) {
Test t = new Test();
try{
t.clone();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
class ABC{
}
,来自类测试的克隆方法 super.clone 正在被调用?
为什么它不抛出异常?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Test
实例t
的继承树看起来像Test
也实现了Cloneable
这意味着当您调用该方法时 < code>super.clone()Object
的clone
方法将被调用。它检查实例t
是否实现了Cloneable
接口。由于它确实实现了该方法,因此不会引发异常。Inheritance tree of the
Test
instancet
looks likeTest
also implementsCloneable
which means that when you call the methodsuper.clone()
Object
'sclone
method will be called. It checks if the instancet
implements theCloneable
interface. Since it does implement the method doesn't throw an exception.Cloneable接口是一个标记接口,表明实现类支持clone方法。 super.clone() 不抛出异常的原因是调用它的基对象是可通过继承克隆的。
来自 Javadoc:
请参阅:可克隆 (Java Platform SE 6)< /a>
The Cloneable interface is a marker interface that indicates that the implementing class supports the clone method. The reason that super.clone() doesn't throw an exception is that the base Object it is being called on is Cloneable by inheritance.
From the Javadoc:
See: Cloneable (Java Platform SE 6)
因为你的类实现了
Cloneable
。来自Javadoc >可克隆
:因此,这允许使用
Object
的clone()
方法。它(同样根据 Javadoc)只是重写clone()
的约定。如果您不重写它并且Object
的clone()
方法执行逐字段复制,您可能仍会收到CloneNotSupportedException
如果其中一个字段本身不可克隆。因此,重写它是一个好建议。Because your class implements
Cloneable
. From the Javadoc ofCloneable
:So this allows to use
Object
'sclone()
method. It is (again according to the Javadoc) just a convention to overrideclone()
. If you do not override it andObject
'sclone()
method performs a field-for-field copy, you might still get aCloneNotSupportedException
if one of the fields is itself not cloneable. Therefore it is good advice to override it.这样,选择支持克隆的类就不会被迫在其公共 API 中公开它。
正如您的示例所示,允许子类重写比该方法的继承版本具有更多访问权限的方法。您只是不能以这种方式减少方法的访问。
这是正确的。
这是正确的。
因为您已经声明您的类实现了
Cloneable
。如果您没有这样做,它会抛出异常...假设您的克隆方法仍然调用super.clone()
。So that a class that chooses to support cloning is not forced to expose it in its public API.
As your example demonstrates, a subclass is allowed to override a method with a more access than the inherited version of the method. You just cannot reduce a method's access this way.
That is correct.
That is correct.
Because you have declared that your class implements
Cloneable
. If you had not done this, it would throw an exception ... assuming that your clone method still calledsuper.clone()
.这可能会告诉您为什么该方法受到保护的答案
为什么克隆方法受到保护
它不抛出异常,因为你实现了 Cloneable。来自可克隆文档:
但是当你实现它时,你告诉对象实现,进行字段到字段的复制是合法的,因此它不会抛出异常。
This might give you the answer to why is the method protected
why is clone method protected
It does not throw exception because you implement Cloneable. From the Cloneable documentation:
But as you're implementing it you tell the Object implementation that it's legal to make a field-for-field copy so it does not throw the exception.
我会问一个好问题来迷惑受访者:)。
至于它不抛出异常的原因。以对象的方式思考它。您的 t 对象属于 Test 类,它实现了可克隆功能。当您调用方法 super.clone 时,它仍然是 Test 类的对象,因此没有理由抛出 CloneNotSupported 异常。如果它是 Object 类本身的对象,它就会抛出异常。
I would say a good question to confuse the interviewees :).
As far as the reason why it doesn't throw the exception. Think of it in object way. Your t object is of class Test which implements cloneable. When you call the method super.clone it is still an object of class Test and so there is no reason for it to throw CloneNotSupported exception. Had it been an object of class Object itself it would have thrown the exception.