Java clone() method

前端 未结 3 1297
慢半拍i
慢半拍i 2021-01-13 18:45

I read Effective Java by J.Bloch and here was wrote:

If you design a class for inheritance, be aware that if you choose not to provide a well-behave

3条回答
  •  我在风中等你
    2021-01-13 19:30

    In your case yes you can override clone():

    public class A {
    }
    
    public class B extends A implements Cloneable {
    
        @Override
        public B clone() throws CloneNotSupportedException {
            return (B) super.clone();
        }
    }
    

    and still have an effective clone mechanism - you are therefore telling the truth when you state implements Cloneable.

    However, all that is needed to break that promise is to give A a private variable.

    public class A {
        private int a;
    }
    

    and now your promise is broken - unless A implements clone, in which case you can use super.clone():

    public class A {
    
        private int a;
    
        @Override
        public A clone() throws CloneNotSupportedException {
            A newA = (A) super.clone();
            newA.a = a;
            return newA;
        }
    }
    
    public class B extends A implements Cloneable {
    
        private int b;
    
        @Override
        public B clone() throws CloneNotSupportedException {
            B newB = (B) super.clone();
            newB.b = b;
            return newB;
        }
    }
    

    Essentially - as Joshua Bloch states - if you don't implement clone your sub-classes also cannot (generally).

提交回复
热议问题