淺轉殖:指在轉殖乙個物件的時候,如果被轉殖的物件中又維護了另乙個物件,這個時候雖然轉殖了成功了,但是僅僅是對另乙個物件的位址轉殖了,並沒有將另乙個物件也轉殖了;
深轉殖:採用io流來實現,使用 objectoutputstream 將物件寫入檔案中,然後再用objectinputstream讀取回來
淺轉殖程式如下:
public
class
mytest
}class
flower implements cloneable
public
flower
(string name, string color, dirt dirt)
@override
protected object clone
() throws clonenotsupportedexception
@override
public string tostring()
';}}
class
dirt
public
dirt
(string name)
@override
public string tostring()
';}}
深轉殖**示例如下:
深轉殖先要將物件,以及其所維護的物件,通過序列化流輸出到文字文件中儲存起來,完成整體轉殖;
public
class
clone
}class
flower
implements
cloneable
, serializable
public
flower
(string name, string color, dirt dirt)
@override
protected object clone()
throws clonenotsupportedexception
@override
public string tostring()
';}}
class
dirt
implements
serializable
public
dirt
(string name)
@override
public string tostring()
';}}
其次要想讀取轉殖出來的物件,用反序列化流再關聯檔案讀取出來;
public
class
clone
}class
flower
implements
cloneable
, serializable
public
flower
(string name, string color, dirt dirt)
@override
protected object clone()
throws clonenotsupportedexception
@override
public string tostring()
';}}
class
dirt
implements
serializable
public
dirt
(string name)
@override
public string tostring()
';}}
轉殖,深轉殖和淺轉殖
轉殖,深轉殖和淺轉殖。1.實現轉殖,繼承cloneable介面,實現clone beanutils.clonebean origin 2.深轉殖和淺轉殖的區別,淺轉殖指的是轉殖的時候僅僅轉殖物件本身 物件的基本變數 而不拷貝物件的引用變數。深轉殖指的是轉殖的時候不僅轉殖物件本身,而且拷貝物件包含的引...
深轉殖和淺轉殖
為什麼需要轉殖?new出來的物件中的屬性都是初始化時候的值,當需要乙個新的物件來儲存當前物件的 狀態 就靠clone方法了。直接使用object a new object object b b a不行嗎,答案是不行的。因為這只是轉殖了引用。如何實現轉殖?首先由兩種轉殖的方式,乙個是深轉殖,乙個是淺轉...
淺轉殖和深轉殖
1淺轉殖 shallow copy 是通過呼叫object的clone方法,建立出和原物件 結構相同的新物件。淺轉殖 和深轉殖 deep copy 不同,如果被拷貝的物件屬性為可變類,則直接複製該引用物件的位址。這會導致,淺轉殖出來的物件 如果改變可變類屬性值,則原物件的可變類屬性值也會一起變。換句...