CodeGym /Kursus Jawa /Multithreading Jawa /Antarmuka panandha lan salinan jero

Antarmuka panandha lan salinan jero

Multithreading Jawa
tingkat , wulangan
kasedhiya
Antarmuka panandha lan salinan jero - 1

"Halo, Amigo!"

"Hai, Bilaabo!"

"Dina iki aku bakal ngandhani sampeyan babagan antarmuka panandha ."

"Antarmuka panandha antarmuka tanpa cara. Nalika kelas ngleksanakake antarmuka kuwi, kita ngomong sing wis ditandhani dening."

"Conto antarmuka iki kalebu: Cloneable, Serializable, Remote ."

" Antarmuka Serializable digunakake kanggo menehi tandha kelas sing ndhukung serialization, nuduhake yen kedadean saka kelas iki bisa otomatis serialized lan deserialized."

" Antarmuka Remote digunakake kanggo ngenali obyek sing ndhukung eksekusi remot, yaiku cara sing bisa dijaluk saka mesin virtual Java liyane lan/utawa komputer liyane."

" Antarmuka Cloneable digunakake kanggo menehi tandha kelas sing ndhukung kloning."

"Oh, babagan kloning, utawa nyalin."

"Ana rong jinis nyalin: cethek lan jero."

" Salinan cethek nggawe salinan obyek, tanpa nggawe duplikat saka obyek sing dirujuk."

" Nyalin jero kalebu duplikat obyek, kalebu obyek sing dirujuk, lan obyek sing referensi obyek kasebut, lsp."

"Ana cara sing apik banget kanggo nggawe klon sing bisa dipercaya."

"Cara iki bisa dianggo sanajan pangembang wis lali kanggo menehi tandha kelas minangka Cloneable. "Sawijining syarat yaiku obyek kudu serializable."

"Mangkene carane sampeyan nindakake:"

1) Nggawe buffer (byte array) ing memori.

2) Serialize obyek lan subobyek menyang buffer.

3) Deserialize hirarki obyek sing disimpen ing buffer.

Kode
BigObject objectOriginal = new BigObject();

ByteArrayOutputStream writeBuffer = new ByteArrayOutputStream();
ObjectOutputStream outputStream = new ObjectOutputStream(writeBuffer);
outputStream.writeObject(objectOriginal);
outputStream.close();

byte[] buffer = writeBuffer.toByteArray();
ByteArrayInputStream readBuffer = new ByteArrayInputStream(buffer);
ObjectInputStream inputStream = new ObjectInputStream(readBuffer);
BigObject objectCopy = (BigObject)inputStream.readObject();

"Ing baris pisanan, kita nggawe objectOriginal , sing bakal kita tiron. Obyek lan kabeh subobyek kudu ndhukung serialisasi."

"Ing baris katelu, kita nggawe ByteArrayOutputStream , sing bakal nggedhekake kanthi dinamis nalika data anyar ditambahake (kaya ArrayList)."

"Ing baris 4, kita nggawe ObjectOutputStream , sing digunakake kanggo serialisasi."

"Ing baris 5, kita serialize objectOriginal menyang array byte nggunakake outputStream lan simpen menyang writeBuffer ."

"Ing baris 8, kita ngowahi writeBuffer dadi array byte biasa. Mengko kita bakal 'maca' obyek anyar saka array iki."

"Ing baris 9, kita ngowahi buffer menyang ByteArrayInputStream supaya bisa diwaca kaya InputStream."

"Ing baris 10, kita ngliwati readBuffer menyang konstruktor ObjectInputStream kanggo maca (deserialize) obyek kasebut."

"Ing baris 11, kita maca obyek lan ngowahi dadi BigObject ."

"Menurutmu piye?"

"Iku ayu."

"Lan, nalika kode kasebut disorot kanthi warna sing beda-beda, luwih gampang dingerteni."

Komentar
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION