从 java.lang.Object 访问 clone()

发布于 2024-10-18 16:19:20 字数 504 浏览 7 评论 0原文

这是我无法理解的事情。

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 技术交流群。

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

发布评论

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

评论(4

年少掌心 2024-10-25 16:19:20

在您的情况下, clone() 方法不可见,因为您没有从子类调用它。 Sample 派生自 Object,因此它可以访问自己的 clone() 方法,但不能访问其他对象的方法。

对象clone() 的设计有几个错误。因此,使用它不是一个好的做法 - 很难做到正确:

  • 假设不是每个对象默认都是可克隆的,
  • 如果您覆盖 clone() 使其公开,它将仍然失败,因为每个类都必须实现Cloneable
  • Cloneable,但是,没有定义任何方法,因此对象的用户无法将其引用为可克隆并期望一个克隆方法。
  • 层次结构中的每个类都必须调用 super.clone() 以使默认克隆机制发挥作用

检查 这个问题作为替代方案。

In your case, the clone() method is not visible because you are not calling it from a subclass. Sample derives from Object, so it can access its own clone() 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:

  • the assumption is that not every object is clonable by default
  • if you override clone() making it public, it will still fail, because each class has to implement Cloneable
  • Cloneable, however, does not define any methods, so the users of the objects can't refer to it as Cloneable and expect a clone method.
  • every class in a hierarchy must call super.clone() for the default cloning mechanism to work

Check this question for alternatives.

帅的被狗咬 2024-10-25 16:19:20

对我有用: http://ideone.com/eST8Y

import java.util.*;
import java.lang.*;

class Main
{
    public Object foo() throws CloneNotSupportedException
    {
        return this.clone();
    }
    
    public static void main (String[] args) throws java.lang.Exception
    {
        new Main().foo();
    }
}

编译没有错误。 它仍然抛出运行时 CloneNotSupportedException 因为 Main 没有实现 可克隆

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

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


@Bozho 的答案确实是正确的答案。 请勿使用 Object.clone()

请参阅有效的 Java,第 10 项:覆盖 明智地克隆(后续版本中的第11条)。

Works for me: http://ideone.com/eST8Y

import java.util.*;
import java.lang.*;

class Main
{
    public Object foo() throws CloneNotSupportedException
    {
        return this.clone();
    }
    
    public static void main (String[] args) throws java.lang.Exception
    {
        new Main().foo();
    }
}

This compiles without error. It still throws a runtime CloneNotSupportedException because Main does not implement 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.

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


@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).

水水月牙 2024-10-25 16:19:20

someObject 的类类型在这里很重要。对象 someObject 可能不会覆盖 Object 类的 clone() 方法,因此 make 对类 Sample 不可见。

The class type of someObject is important here. Object someObject might not be overriding the clone() method of Object class, thus making is invisible to class Sample.

难如初 2024-10-25 16:19:20

“无法访问”是什么意思?你的意思是它不会编译还是你的意思是它抛出 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 throw CloneNotSupportedException if your class doesn't implement the Cloneable interface.

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