Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么clone()方法在java.lang.Object中受到保护?_Java_Oop_Clone - Fatal编程技术网

为什么clone()方法在java.lang.Object中受到保护?

为什么clone()方法在java.lang.Object中受到保护?,java,oop,clone,Java,Oop,Clone,在java.lang.Object中定义为受保护的具体原因是什么?clone受到保护,因为它是应该被重写的对象,因此它是特定于当前类的。虽然可以创建一个公共的克隆方法来克隆任何对象,但这不如为需要它的类专门编写的方法好。clone受到保护,因为它是应该被覆盖的,因此它是特定于当前类的。虽然可以创建一个公共的clone方法来克隆任何对象,但这不如为需要它的类专门编写的方法好。克隆受到保护的事实是非常可疑的,因为clone方法没有在Cloneable接口 它使该方法对于复制数据非常无用,因为您不能说

java.lang.Object
中定义为受保护的具体原因是什么?

clone
受到保护,因为它是应该被重写的对象,因此它是特定于当前类的。虽然可以创建一个公共的克隆方法来克隆任何对象,但这不如为需要它的类专门编写的方法好。

clone
受到保护,因为它是应该被覆盖的,因此它是特定于当前类的。虽然可以创建一个公共的
clone
方法来克隆任何对象,但这不如为需要它的类专门编写的方法好。

克隆受到保护的事实是非常可疑的,因为
clone
方法没有在
Cloneable
接口

它使该方法对于复制数据非常无用,因为您不能说

if(a instanceof Cloneable) {
    copy = ((Cloneable) a).clone();
}
我认为,
Cloneable
的设计现在在很大程度上被认为是一个错误(引文如下)。我通常希望能够实现接口
可克隆
,但不一定要使接口
可克隆
(类似于使用
可序列化
)。如果没有思考,就无法做到这一点:

ISomething i = ...
if (i instanceof Cloneable) {
   //DAMN! I Need to know about ISomethingImpl! Unless...
   copy = (ISomething) i.getClass().getMethod("clone").invoke(i);
}
引用Josh Bloch的《有效的Java》

“Cloneable接口的目的是作为对象的mixin接口,以公布它们允许克隆。不幸的是,它无法达到这一目的……这是一种非常非典型的接口使用,而不是一个可仿真的接口……为了实现接口对类产生任何影响,它及其所有超类必须遵守相当复杂、不可执行且大部分未记录的协议


克隆受到保护这一事实极为可疑,因为
clone
方法没有在
Cloneable
界面中声明

它使该方法对于复制数据非常无用,因为您不能说

if(a instanceof Cloneable) {
    copy = ((Cloneable) a).clone();
}
我认为,
Cloneable
的设计现在在很大程度上被认为是一个错误(引文如下)。我通常希望能够实现接口
可克隆
,但不一定要使接口
可克隆
(类似于使用
可序列化
)。如果没有思考,就无法做到这一点:

ISomething i = ...
if (i instanceof Cloneable) {
   //DAMN! I Need to know about ISomethingImpl! Unless...
   copy = (ISomething) i.getClass().getMethod("clone").invoke(i);
}
引用Josh Bloch的《有效的Java》

“Cloneable接口的目的是作为对象的mixin接口,以公布它们允许克隆。不幸的是,它无法达到这一目的……这是一种非常非典型的接口使用,而不是一个可仿真的接口……为了实现接口对类产生任何影响,它及其所有超类必须遵守相当复杂、不可执行且大部分未记录的协议


Clone方法不能直接用于任何对象,这就是为什么它要被子类覆盖的原因

当然,它可能是公开的,当克隆不可能的时候,它会抛出一个适当的例外,但我认为这会产生误导


现在实现克隆的方式让您思考为什么要使用克隆,以及如何克隆对象。

克隆方法不能直接用于任何对象,这就是为什么它要被子类覆盖

当然,它可能是公开的,当克隆不可能的时候,它会抛出一个适当的例外,但我认为这会产生误导


现在实现克隆的方式让您思考为什么要使用克隆,以及如何克隆对象。

它受到保护,因为默认实现对所有字段(包括私有字段)进行浅成员复制,从而绕过构造函数。这并不是一个对象一开始就可以处理的事情(例如,它可能会在共享列表中跟踪创建的对象实例,或者类似的事情)


出于同样的原因,如果调用它的对象没有实现
Cloneable
,则
clone()的默认实现将抛出。这是一个潜在的不安全操作,具有深远的后果,因此类的作者必须明确地选择加入。

它受到保护,因为默认实现对所有字段(包括私有字段)进行浅成员复制,从而绕过构造函数。这并不是一个对象一开始就可以处理的事情(例如,它可能会在共享列表中跟踪创建的对象实例,或者类似的事情)


出于同样的原因,如果调用它的对象没有实现
Cloneable
,则
clone()的默认实现将抛出。这是一个具有深远影响的潜在不安全操作,因此该类的作者必须明确选择加入。

来自cloneable的javadoc

* By convention, classes that implement this interface (cloneable) should override 
* <tt>Object.clone</tt> (which is protected) with a public method.
* See {@link java.lang.Object#clone()} for details on overriding this
* method.

* Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
* Therefore, it is not possible to clone an object merely by virtue of the
* fact that it implements this interface.  Even if the clone method is invoked
* reflectively, there is no guarantee that it will succeed.
*按照惯例,实现此接口(可克隆)的类应该重写
*Object.clone(受保护)使用公共方法。
*请参阅{@link java.lang.Object#clone()}了解有关重写此项的详细信息
*方法。
*请注意,此接口不包含克隆方法。
*因此,不可能仅仅凭借
*事实上,它实现了这个接口。即使调用了clone方法
*反省一下,这并不能保证它会成功。

因此,您可以对每个对象调用clone,但这将在大多数情况下为您提供所需的结果或异常。但是,只有在您实现cloneable时,才鼓励使用

来自cloneable的javadoc

* By convention, classes that implement this interface (cloneable) should override 
* <tt>Object.clone</tt> (which is protected) with a public method.
* See {@link java.lang.Object#clone()} for details on overriding this
* method.

* Note that this interface does <i>not</i> contain the <tt>clone</tt> method.
* Therefore, it is not possible to clone an object merely by virtue of the
* fact that it implements this interface.  Even if the clone method is invoked
* reflectively, there is no guarantee that it will succeed.
*按照惯例,实现