我对在想要克隆对象的类中重写克隆方法感到困惑。
Object类有一个受保护的对象方法,根据受保护的行为,即当一个方法受到保护时,它只能被该类本身、该类的子类或与该类在同一包中的类访问。
由于Java中的每个类都是从Object类继承而来的,所以它应该具有克隆方法,但我们仍然被迫重写clone方法。这是为什么呢?
此外,我在某些地方读到过要重写克隆对象并使其公开的建议。我想知道,为什么需要这样做?
欢迎所有答案。
我对在想要克隆对象的类中重写克隆方法感到困惑。
Object类有一个受保护的对象方法,根据受保护的行为,即当一个方法受到保护时,它只能被该类本身、该类的子类或与该类在同一包中的类访问。
由于Java中的每个类都是从Object类继承而来的,所以它应该具有克隆方法,但我们仍然被迫重写clone方法。这是为什么呢?
此外,我在某些地方读到过要重写克隆对象并使其公开的建议。我想知道,为什么需要这样做?
欢迎所有答案。
public class A {
private int data;
public A() {
}
public A(A a) {
this.data = a.data;
}
}
如果需要更多细节,我建议阅读Joshua Bloch
的《Effective Java》中的这一章节,它涵盖了使用clone
方法的所有方面。
protected clone
方法。因此,“Cloneable”只是标记接口,并没有自己的'clone'方法。因此,我们必须使用Object类的clone
方法。所以,你不能仅仅因为一个对象实现了Cloneable就调用克隆方法。有关更多详细信息,我建议你查看我提供的书籍链接。那是理解这个问题的最佳资源。 - Rohit Jainpublic class Foo {
private String name;
public Foo(String name) { this.name = name; }
public Foo(Foo f) { this.name = f.name; } // copy ctor here.
}
克隆是Object
类中的protected
方法,因此您可以在类内部访问它。
关于访问权限-当一个方法被保护时,只有类本身、类的子类或与类相同包中的类才能访问该方法。
我看到了一些关于克隆方法的误解
clone()
方法在Object
类中是protected
的,因此你不能在类外调用child.clone()
等方法,除非你重写它并使访问权限为public
Cloneable
是标记接口,如果您不将类标记为Cloneable
,则在调用clone()
方法时会出现CloneNotSupportedException
super.clone
返回的对象中的任何字段。super.clone
来获取返回的对象。如果一个类及其所有的超类(除了Object)
都遵循这个惯例,那么就会有x.clone().getClass() == x.getClass()
。方法签名如下
@Override
public Object clone() throws CloneNotSupportedException {
return super.clone();
}
参考资料:
Why we do override clone() in cloning process?
//clone() in Object class is protected
package java.lang;
protected native Object clone()
throws CloneNotSupportedException;
java.lang is default import in our java applications.
Note: If parent and sub class are both in same package then the methods in parent class are directly accessible. If they are in different package,then in subclass we have to override the parent class methods to use.
Note:Object class is in java.lang package,we are using it in different package,so we have to override the clone() which is protected in Object class
first we will look into Protected method behavior.here is sample program to understand this
//this class is in com.anusha.clonetrial
package com.anusha.clonetrial;
public class A {
public A()
{
}
protected void disp1()
{
System.out.println("class a");
}
protected void disp2()
{
System.out.println("class a");
}
}
//below classes are in com.anusha.Test
package com.anusha.Test;
import com.anusha.clonetrial.A;
class AA {
protected void disp1()
{
System.out.println("class aa");
}
protected void disp2()
{
System.out.println("class aa");
}
}
//class B derived from AA which is present in the same package
class B extends AA
{
void show()
{
System.out.println("class b");
}
}
//class C derived from A which is present in the different package
class C extends A
{
@Override
protected void disp1()
{
super.disp1();
}
void show()
{
System.out.println("class c");
}
}
package com.anusha.Test;
public class CloneTest {
public static void main(String[] args) {
B b=new B();
C c=new C();
b.disp1();
b.disp2();
c.disp1();
c.disp2();//gives error because it is not overridden.
}
}