The method clone() from object is not visible?

后端 未结 8 676
半阙折子戏
半阙折子戏 2020-12-08 06:46

Question:

package GoodQuestions;
public class MyClass {  
    MyClass() throws CloneNotSupportedException {
        try {
            throw new CloneNotSuppo         


        
相关标签:
8条回答
  • 2020-12-08 07:16

    You just have to make MyClass implement Cloneable interface. No need to provode implementation for clone().

    0 讨论(0)
  • 2020-12-08 07:16

    Object.clone() method has protected access, meaning it's visible to sub-classes and classes in the same package.

    It's good to have a copy constructor for manually copying the object.

    /**
        Deep copy all the information from other to this
    */
    public MyClass (MyClass  other) {
         this.id = other.id;
    }
    

    READ Why a copy constructor from Josh Bloch

    0 讨论(0)
提交回复
热议问题