How to properly override clone method?

后端 未结 9 2286
时光说笑
时光说笑 2020-11-22 12:06

I need to implement a deep clone in one of my objects which has no superclass.

What is the best way to handle the checked CloneNotSupportedException thr

9条回答
  •  鱼传尺愫
    2020-11-22 12:37

    Sometimes it's more simple to implement a copy constructor:

    public MyObject (MyObject toClone) {
    }
    

    It saves you the trouble of handling CloneNotSupportedException, works with final fields and you don't have to worry about the type to return.

提交回复
热议问题