物件序列化

2021-08-14 18:22:28 字數 831 閱讀 6464

序列化可以實現深拷貝

1. 深拷貝

複製物件將所有原物件引用的物件全部複製乙份。注意是所有的引用物件,包括引用物件的引用物件都會複製乙份的。這個在**的實驗中證明了。詳見另一篇部落格 設計模式——原型模式——深拷貝

在attachment中加入了cat物件,cat物件序列化後== 結果是false

將物件寫入檔案流或者字元流中**

public

class test

}class objectstream catch (ioexception e)

}public

static object readobject(string s) catch (filenotfoundexception e) catch (ioexception e) catch (classnotfoundexception e)

return

null;

}}class dog implements serializable

public

dog()

@override

public string tostring() ';

}public string getname()

public

void

setname(string name)

public

intgetage()

public

void

setage(int age)

}

輸出:

dog

false

false

物件序列化 反序列化

必須新增引用 using system.io using system.runtime.serialization using system.runtime.serialization.formatters.binary 方法 region 物件序列化 物件序列化 任意物件 字串 public st...

物件 序列化 反序列化

public class eventmessage 資訊提示類 型別1 操作日誌2 安全日誌 標題 內容 icon型別 url 執行script指令碼字串 需加 public static void messagebox int m type,string m title,string m body...

物件序列化

using system using system.io using system.diagnostics using system.reflection using system.runtime.serialization using system.runtime.serialization.fo...