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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java 序列化声明_显式声明默认Java类序列化方法的原因是什么?

發布時間:2025/3/8 java 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 序列化声明_显式声明默认Java类序列化方法的原因是什么? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我定期看到具有以下結構的Java類:

class MyClass implements Serializable {

private static final long serialVersionUID = 1L;

// ...

private void writeObject(final java.io.ObjectOutputStream s) throws IOException {

s.defaultWriteObject();

}

private void readObject(final java.io.ObjectInputStream s) throws IOException, ClassNotFoundException {

s.defaultReadObject();

}

}

據我所知,對于默認序列化,您只需要聲明可序列化接口的實現并定義serialVersionUID.是否有任何理由使用默認實現聲明寫/讀方法?我厭倦了關于代碼重復的分析器警告.

沒有看到任何關于這個主題的非常明確的指南.一切都有點過時或非常主觀.

解決方法:

ObjectInputStream#defaultReadObject和ObjectOutputStream#defaultWriteObject方法適用于您希望使用默認值編寫字段以及您有其他讀取或寫入信息,或者您想要修改讀取或寫入內容的情況.如果您不想添加或更改任何內容,則僅在實現Serializable時添加顯式調用沒有任何優勢.

The default mechanism is used automatically when reading or writing objects that implement the Serializable interface and do no further customization. The serializable fields are mapped to the corresponding fields of the class and values are either written to the stream from those fields or are read in and assigned respectively. If the class provides writeObject and readObject methods, the default mechanism can be invoked by calling defaultWriteObject and defaultReadObject. When the writeObject and readObject methods are implemented, the class has an opportunity to modify the serializable field values before they are written or after they are read.

標簽:java,serialization

來源: https://codeday.me/bug/20190623/1274141.html

總結

以上是生活随笔為你收集整理的java 序列化声明_显式声明默认Java类序列化方法的原因是什么?的全部內容,希望文章能夠幫你解決所遇到的問題。

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