java.lang.CloneNotSupportedException

it2024-04-16  48

使用clone创建对象发现报了CloneNotSupportedException错误

首先找到错误的源头:

protected native Object clone() throws CloneNotSupportedException;

看了一眼源码的注释,秒懂:

* @throws CloneNotSupportedException if the object's class does not * support the {@code Cloneable} interface. Subclasses * that override the {@code clone} method can also * throw this exception to indicate that an instance cannot * be cloned.

翻译过来就是如果对象没有实现Cloneable接口,即使复写了clone方法还是会抛出CloneNotSupportedException异常,所以很简单,实现一下Cloneable接口就可以了

最新回复(0)