拾贝-Java 工具类整理

目标:整理常用工具


目录:

1. clone 方法

 

2021-02-22

1. clone 方法, 其他类 extends 即可. 抛出 CloneNotSupportedException.

 1 public class SerialCloneable implements Cloneable, Serializable {
 2 
 3     public Object clone() throws CloneNotSupportedException{
 4         try{
 5             var bout = new ByteArrayOutputStream();
 6             try(var out = new ObjectOutputStream(bout)){
 7                 out.writeObject(this);
 8             }
 9 
10             try(var bin = new ByteArrayInputStream(bout.toByteArray())){
11 
12                 var in = new ObjectInputStream(bin);
13                 return in.readObject();
14             }
15         }catch (IOException | ClassNotFoundException e){
16             var e2 = new CloneNotSupportedException();
17             e2.initCause(e);
18             throw e2;
19         }
20     }
21 }

 

posted @ 2021-02-22 22:55  君子之行  阅读(5)  评论(0)    收藏  举报