Java类中为什么不能直接调用Object的clone()方法
黄舟
黄舟 2017-04-18 10:16:27
0
5
1071

在Java中所有的类都是Object的子类。

在Object类中有一个clone方法定义如下:

protected native Object clone() throws CloneNotSupportedException;

该方法的修饰符为protected,表示该方法可以在子类中调用


然后结果是调用不了

网上有回答是需要实现Cloneable接口,但即使实现了,也调用不到。
不实现Cloneable接口,只是报CloneNotSupportedException异常。

只能重写clone方法,并且使用super.clone()

疑惑这是为什么呢?

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(5)
巴扎黑

The Cloneable interface is just a flag, it is empty inside.
The clone method of Object is a local method, which is more efficient.
Several conditions for using the clone method

1)在派生类中实现Cloneable借口。

 2) In order to obtain a copy of the object, we can use the clone method of the Object class.

 3) Override the accumulated clone method in the derived class and declare it as public.

 4) In the clone method of the derived class, call super.clone().

For more details, you can refer to
http://www.cnblogs.com/gw811/...

PHPzhong

You can call:

public class Test implements Cloneable{
    private int foo;

    public Test(int foo) {
        this.foo = foo;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public int getFoo() {
        return foo;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        Test test = new Test(1);
        Test cloned = (Test) test.clone();
        System.out.println(cloned.getFoo());
    }
}
刘奇

clone() is a protected scope. After inheriting the Cloneable interface, you need to override the method, and then call the clone() method of the parent class in the method. At the same time, the default clone is only a shallow clone of the reference object. Let me give you a piece of code to run and try it yourself:

package cesar.Test0810;

/**
 * Created by Cesar on 2016/8/10.
 */
public class A implements Cloneable{

    private int a;
    private B b;

    public int getA() {
        return a;
    }

    public void setA(int a) {
        this.a = a;
    }

    public B getB() {
        return b;
    }

    @Override
    public String toString() {
        return "A{" +
                "a=" + a +
                ", b=" + b +
                '}';
    }

    public void setB(B b) {
        this.b = b;
    }

    protected A clone() throws CloneNotSupportedException {
        return (A) super.clone();
    }
}
package cesar.Test0810;

/**
 * Created by Cesar on 2016/8/10.
 */
public class B {

    private int b1;
    private int b2;

    @Override
    public String toString() {
        return "B{" +
                "b1=" + b1 +
                ", b2=" + b2 +
                '}';
    }

    public B(int b1, int b2){
        this.b1 = b1;
        this.b2 = b2;
    }

    public int getB1() {
        return b1;
    }

    public void setB1(int b1) {
        this.b1 = b1;
    }

    public int getB2() {
        return b2;
    }

    public void setB2(int b2) {
        this.b2 = b2;
    }
}
package cesar.Test0810;

/**
 * Created by Cesar on 2016/8/10.
 */
public class TestClone {

    public static void main(String[] args) {

        A a = new A();
        B b = new B(1, 2);

        a.setA(10);
        a.setB(b);
        try {
            A a1 = a.clone();
            System.out.println("a=" + a.toString());
            System.out.println("a1=" + a1.toString());

            a.setA(1000);
            a.getB().setB1(10000);
            a.getB().setB2(8000);

            System.out.println("a=" + a.toString());
            System.out.println("a1=" + a1.toString());

            a.setB(new B(9999,9999));

            System.out.println("a=" + a.toString());
            System.out.println("a1=" + a1.toString());


        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }
}
  • A inherits the cloneable interface and holds a reference to B.

Ty80

Implement the interface Cloneable and override the clone() method.

Peter_Zhu

I was very confused when I first saw it. I tried it and found that the clone method in the object was directly called.
The code is as follows:

public class CommonTest implements Cloneable{

public static String name  = "hell0";
public static void main(String[] args){

    try{
        CommonTest aa = new CommonTest();
        CommonTest ee = (CommonTest) aa.clone();
        System.out.println("Clone succeed");
        System.out.println(ee.name);
    }catch (CloneNotSupportedException e) {
        System.out.print("clone failed");
    }
}

}

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!