日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java中定义一个CloneUtil 工具类

發布時間:2023/12/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java中定义一个CloneUtil 工具类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

其實所有的java對象都可以具備克隆能力,只是因為在基礎類Object中被設定成了一個保留方法(protected),要想真正擁有克隆的能力,

就需要實現Cloneable接口,重寫clone方法。通過克隆方法得到的對象是一個本地的副本。

1、實現Cloneable接口

具體看代碼:

class User implements Cloneable{

  int age;

  public User(int age){

    //用this關鍵字不至于類成員變量與形參混淆

    this.age=age;

  }

  //這里可加@Override也可不加,加了會自動提示是否正確

  @Override

  public?Object clone{

    Object o=null;

    try{

      o=super.clone();

    }catch(CloneNotSupportedException e){

      e.printStackTrace();

    }

  }

  public String toString(){

    return Integer.toString(this.age);

  }

}

2、自己手動寫一個CloneUtil 工具類,不是實現Cloneable這種方式,但是能達到效果。

package cn.com.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

/**
?* 對象clone 工具類
?* @author
?*
?*/
public class CloneUtil {
?? ?public static Object clone(Serializable obj) {
?? ??? ?Object clone = cloneObject(obj);
?? ??? ?if (clone == null) {
?? ??? ??? ?clone = cloneObject(obj);
?? ??? ?}
?? ??? ?return clone;
?? ?}

?? ?public static Object cloneObject(Serializable obj) {
?? ??? ?Object anotherObj = null;

?? ??? ?ByteArrayOutputStream baos = new ByteArrayOutputStream();
?? ??? ?ObjectOutputStream oos = null;

?? ??? ?ObjectInputStream ois = null;
?? ??? ?try {
?? ??? ??? ?oos = new ObjectOutputStream(baos);
?? ??? ??? ?oos.writeObject(obj);
?? ??? ??? ?byte[] bytes = baos.toByteArray();

?? ??? ??? ?ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

?? ??? ??? ?ois = new ObjectInputStream(bais);
?? ??? ??? ?anotherObj = ois.readObject();
?? ??? ?} catch (IOException ex) {
?? ??? ??? ?throw new RuntimeException(ex.getMessage(), ex);
?? ??? ?} catch (ClassNotFoundException ex) {
?? ??? ??? ?throw new RuntimeException(ex.getMessage(), ex);
?? ??? ?} catch (StackOverflowError error) {
?? ??? ??? ?System.out.println("stack length " + error.getStackTrace().length);
?? ??? ??? ?error.printStackTrace();
?? ??? ??? ?return null;
?? ??? ?} finally {
?? ??? ??? ?if (oos != null)
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?oos.close();
?? ??? ??? ??? ?} catch (IOException localIOException3) {
?? ??? ??? ??? ?}
?? ??? ??? ?if (ois != null)
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?ois.close();
?? ??? ??? ??? ?} catch (IOException localIOException4) {
?? ??? ??? ??? ?}
?? ??? ?}
?? ??? ?return anotherObj;
?? ?}

?? ?public static int getObjectSize(Serializable obj) {
?? ??? ?ByteArrayOutputStream bs = new ByteArrayOutputStream();
?? ??? ?try {
?? ??? ??? ?ObjectOutputStream os = new ObjectOutputStream(bs);
?? ??? ??? ?os.writeObject(obj);
?? ??? ??? ?os.flush();
?? ??? ?} catch (Exception e) {
?? ??? ??? ?throw new RuntimeException(e.getMessage(), e);
?? ??? ?}
?? ??? ?return bs.size();
?? ?}
}

?

總結

以上是生活随笔為你收集整理的java中定义一个CloneUtil 工具类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。