網站首頁 編程語言 正文
CloneNotSupportedException的解決方案
引入問題:
在一次測試clone方法時,D類Override了Object類的clone方法
public class D {
private Integer A1;
private Integer A2;
public D() {
}
public D(Integer a1, Integer a2 {
A1 = a1;
A2 = a2;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
123456789101112131415161718
在測試類中執行clone方法報錯了
public class Test5 {
public static void main(String[] args) {
D p = new D();
D p2 = null;
try {
p2 = (D)p.clone();
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
System.out.println(p == p2);
}
}
12345678910111213141516
拋出了 CloneNotSupportedException
尋找原因:
既然是重寫自Object的clone方法,那么去看看Object對該方法的定義:
protected native Object clone() throws CloneNotSupportedException;
1
其中對于異常拋出的描述如下:
Throws:
CloneNotSupportedException – if the object’s class does not support the Cloneable interface. Subclasses that override the clone method can also throw this exception to indicate that an instance cannot be cloned.
如果對象的class不實現Cloneable接口,子類重寫clone方法會同樣拋出異常表示實例不能被克隆;
哦?
那不意思就是說:你要不父類實現Cloneable接口,要不就子類實現Cloneable
兩個測試代碼:
測試1: 在父類上實現Cloneable接口
public class TestClone {
public static void main(String[] args) throws CloneNotSupportedException {
Father father = new Father();
Father fatherClone = (Father) father.clone();
Son son = new Son();
Son sonClone = (Son) son.clone();
System.out.println(father==fatherClone);
System.out.println(son==sonClone);
}
}
class Father implements Cloneable{
private Integer f1;
private Integer f2;
public Father() {
}
public Father(Integer f1, Integer f2) {
this.f1 = f1;
this.f2 = f2;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Son extends Father{
private Integer s1;
private Integer s2;
public Son() {
}
public Son(Integer s1, Integer s2) {
this.s1 = s1;
this.s2 = s2;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
不出意外,沒有任何問題
測試2:在子類上實現Cloneable接口
public class TestClone {
public static void main(String[] args) throws CloneNotSupportedException {
// Father father = new Father();
// Father fatherClone = (Father) father.clone();
Son son = new Son();
Son sonClone = (Son) son.clone();
// System.out.println(father==fatherClone);
System.out.println(son==sonClone);
}
}
class Father {
private Integer f1;
private Integer f2;
public Father() {
}
public Father(Integer f1, Integer f2) {
this.f1 = f1;
this.f2 = f2;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
class Son extends Father implements Cloneable{
private Integer s1;
private Integer s2;
public Son() {
}
public Son(Integer s1, Integer s2) {
this.s1 = s1;
this.s2 = s2;
}
@Override
protected Object clone() throws CloneNotSupportedException {
return super.clone();
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
執行父類的clone報錯,但是執行子類的clone沒得問題
SO:
哪個類的需要clone,就在那個類在重寫clone方法,并且在那個類或者其父類中實現Cloneable接口
詳細分析clone方法的實現:
由于Object的clone方法是native修飾的,也就是虛擬機內部實現的方法
根據Hotspot虛擬機定義,找到Object.c
為什么要實現Cloneable接口?
在JVM_Clone方法中有這樣一段判斷:
很明顯,虛擬機給你拋出的異常
為什么會出現地址不同的情況?
很明顯,直接在堆內存中執行allocate方法開辟新的對象空間
并返回一個 oop 對象,也就是對象描述的指針(指向對象的首地址)
原文鏈接:https://blog.csdn.net/qq_43985303/article/details/135315353
- 上一篇:沒有了
- 下一篇:沒有了
相關推薦
- 2024-07-18 Spring Security之認證過濾器
- 2022-09-13 go語言中切片Slice與數組Array對比以及panic:?runtime?error:?inde
- 2021-12-05 密碼學之apache部署https介紹_Linux
- 2022-10-14 laravel 中關于模型查詢構造器的特殊用法
- 2022-01-10 Set、Map、WeakSet 和 WeakMap 的區別?
- 2022-02-21 小程序數據延遲時,可使用userInfoReadyCallback解決
- 2022-06-17 Android啟動頁設置及動態權限跳轉問題解決_Android
- 2022-05-20 Docker容器鏡像加載及底層基本原理深入解析_docker
- 欄目分類
-
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支