可克隆的行为

发布于 2024-09-14 08:03:37 字数 780 浏览 3 评论 0 原文

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 正在被调用?

为什么它不抛出异常?

Java doc says -

The class Object does not itself
implement the interface Cloneable, so
calling the clone method on an object
whose class is Object will result in
throwing an exception at run time.

Which is why clone method in Object class is protected ? is that so ?

That means any class which doesn't implement cloneable will throw CloneNotSupported exception when its clone method is invoked.

I ran a test program

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{

}

From Class Test's clone method super.clone is being invoked ?

Why doesn't it throw exception ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

无名指的心愿 2024-09-21 08:03:37

Test 实例 t 的继承树看起来像

Object
  ABC
    Test

Test 也实现了 Cloneable 这意味着当您调用该方法时 < code>super.clone() Objectclone 方法将被调用。它检查实例t是否实现了Cloneable接口。由于它确实实现了该方法,因此不会引发异常。

Inheritance tree of the Test instance t looks like

Object
  ABC
    Test

Test also implements Cloneable which means that when you call the method super.clone() Object's clone method will be called. It checks if the instance t implements the Cloneable interface. Since it does implement the method doesn't throw an exception.

╰ゝ天使的微笑 2024-09-21 08:03:37

Cloneable接口是一个标记接口,表明实现类支持clone方法。 super.clone() 不抛出异常的原因是调用它的基对象是可通过继承克隆的。

来自 Javadoc:

类实现 Cloneable 接口,以向 Object.clone() 方法指示该方法对该类的实例进行逐字段复制是合法的。

在未实现Cloneable接口的实例上调用Object的clone方法会导致抛出异常CloneNotSupportedException。

请参阅:可克隆 (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:

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.

Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

See: Cloneable (Java Platform SE 6)

烟凡古楼 2024-09-21 08:03:37

因为你的类实现了Cloneable。来自 Javadoc >可克隆

类实现 Cloneable 接口,以向 Object.clone() 方法指示该方法对该类的实例进行逐字段复制是合法的。

因此,这允许使用 Objectclone() 方法。它(同样根据 Javadoc)只是重写 clone() 的约定。如果您不重写它并且 Objectclone() 方法执行逐字段复制,您可能仍会收到 CloneNotSupportedException如果其中一个字段本身不可克隆。因此,重写它是一个好建议。

Because your class implements Cloneable. From the Javadoc of Cloneable:

A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.

So this allows to use Object's clone() method. It is (again according to the Javadoc) just a convention to override clone(). If you do not override it and Object's clone() method performs a field-for-field copy, you might still get a CloneNotSupportedException if one of the fields is itself not cloneable. Therefore it is good advice to override it.

月下客 2024-09-21 08:03:37

为什么Object类中的clone方法受到保护

这样,选择支持克隆的类就不会被迫在其公共 API 中公开它。

正如您的示例所示,允许子类重写比该方法的继承版本具有更多访问权限的方法。您只是不能以这种方式减少方法的访问。

这意味着任何没有实现cloneable的类在调用其clone方法时都会抛出CloneNotSupported异常。

这是正确的。

从 Class Test 的克隆方法 super.clone 被调用?

这是正确的。

为什么不抛出异常?

因为您已经声明您的类实现了Cloneable。如果您没有这样做,它抛出异常...假设您的克隆方法仍然调用super.clone()

Why is the clone method in Object class protected?

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 means any class which doesn't implement cloneable will throw CloneNotSupported exception when its clone method is invoked.

That is correct.

From Class Test's clone method super.clone is being invoked ?

That is correct.

Why doesn't it throw exception ?

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 called super.clone().

朱染 2024-09-21 08:03:37

这可能会告诉您为什么该方法受到保护的答案
为什么克隆方法受到保护

它不抛出异常,因为你实现了 Cloneable。来自可克隆文档:

“一个类实现了 Cloneable 接口,以向 Object.clone() 方法指示该方法对该类的实例进行逐字段复制是合法的。
未实现Cloneable 接口的实例上调用Object 的clone 方法会导致抛出CloneNotSupportedException 异常。 ”

但是当你实现它时,你告诉对象实现,进行字段到字段的复制是合法的,因此它不会抛出异常。

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:

"A class implements the Cloneable interface to indicate to the Object.clone() method that it is legal for that method to make a field-for-field copy of instances of that class.
Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown. "

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.

复古式 2024-09-21 08:03:37

我会问一个好问题来迷惑受访者:)。

至于它不抛出异常的原因。以对象的方式思考它。您的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文