How to properly override clone method?

后端 未结 9 2174
时光说笑
时光说笑 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:44

    The way your code works is pretty close to the "canonical" way to write it. I'd throw an AssertionError within the catch, though. It signals that that line should never be reached.

    catch (CloneNotSupportedException e) {
        throw new AssertionError(e);
    }
    

提交回复
热议问题