CloneNotSupportedException解决方法
程序员文章站
2022-04-15 18:06:44
...
报错的代码:
public class CloneTest {
private Integer id;
public CloneTest(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "CloneTest{" +
"id=" + id +
'}';
}
public static void main(String[] args) throws CloneNotSupportedException {
CloneTest cloneTest = new CloneTest(1);
CloneTest cloneTest1 = (CloneTest)cloneTest.clone();
System.out.println(cloneTest);
System.out.println(cloneTest1);
}
}
原因是该类没有实现Cloneable接口,
修改成如下:
public class CloneTest implements Cloneable {
输出:
CloneTest{id=1}
CloneTest{id=1}
上一篇: @JsonFormat与@DateTimeFormat注解的使用
下一篇: Error running Application. Command line is too long. Shorten the command line via JAR manifest or vi
推荐阅读