search

Home  >  Q&A  >  body text

Java类中为什么不能直接调用Object的clone()方法

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

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

protected native Object clone() throws CloneNotSupportedException;

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


然后结果是调用不了

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

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

疑惑这是为什么呢?

黄舟黄舟2944 days ago1144

reply all(5)I'll reply

  • 巴扎黑

    巴扎黑2017-04-18 10:18:27

    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/...

    reply
    0
  • PHPz

    PHPz2017-04-18 10:18:27

    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());
        }
    }

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:18:27

    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.

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:18:27

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

    reply
    0
  • 天蓬老师

    天蓬老师2017-04-18 10:18:27

    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");
        }
    }

    }

    reply
    0
  • Cancelreply