บทความนี้อธิบายฟังก์ชั่นการจำลองแบบ Java Deep และการใช้งาน แบ่งปันสำหรับการอ้างอิงของคุณดังนี้:
เขียนก่อน:
การจำลองแบบลึกคืออะไร? ใน Java เมื่อสร้างวัตถุเรามักจะมีการอ้างอิงชี้ไปที่วัตถุ เมื่อเราเปลี่ยนค่า (คุณสมบัติ) ของวัตถุโดยอ้างถึงตัวแปรการอ้างอิงยังคงไม่เปลี่ยนแปลงและจะเปลี่ยนหน่วยความจำในหน่วยความจำนั่นคือวัตถุที่ชี้ไป โดยทั่วไปเมื่อเรากำหนดการอ้างอิงนี้ให้กับตัวแปรอ้างอิงอื่นหรือส่งผ่านเป็นพารามิเตอร์เราผ่านมันเท่านั้นนั่นคือเราชี้การอ้างอิงไปที่ "คัดลอก" สำเนาไปยังตัวแปรอ้างอิงอื่นและจากนั้นตัวแปรอ้างอิงยังชี้ไปที่วัตถุเดียวกันและไม่มีวัตถุใหม่ถูกสร้างขึ้นในหน่วยความจำ ในบางกรณีเราต้อง "คัดลอก" วัตถุจริง ๆ สร้างสำเนาของวัตถุที่รู้จักไม่ใช่แค่ "คัดลอก" การอ้างอิงไม่ว่าจะเป็นการสำรองข้อมูลหรือการดำเนินการอื่น ๆ
แล้วจะทำอย่างไร?
มาพูดคุยเกี่ยวกับความคิดก่อน: เป็นครั้งแรกที่ทำให้วัตถุเป็นสตรีมในสตรีมจากนั้น deserialize มันและอ่านออกจากสตรีม
รหัสต่อไปนี้:
แพ็คเกจ com.yo.java; นำเข้า java.io.ByTearrayInputStream; นำเข้า java.io.ByTeArrayOutputStream; นำเข้า java.io.ioException; นำเข้า java.io.ObjectInputStream; นำเข้า java.io.oBjectOutputStream; java.util.list;/** * java ใช้สำเนาลึก * @author yo * */คลาสสาธารณะ DEEPCOPY ใช้ serializable {int i; / ** * @param args * @throws ioexception * @throws classnotfoundexception */ โมฆะคงที่สาธารณะหลัก (สตริง [] args) พ่น classnotfoundexception, ioexception {demo1 (); demo2 (); }/** * สำเนาลึกคลาสพารามิเตอร์จริงจะต้องใช้อินเตอร์เฟส serializable * @param o * @return * @throws ioexception * @throws classnotfoundexception */วัตถุคงที่สาธารณะ deepcopy (Object O) โยน ioException ObjectOutputStream OO = ใหม่ ObjectOutputStream (BO); oo.writeObject (o); // จากนั้น deserialize และอ่านจากสตรีมนั่นคือการคัดลอก ByteArrayInputStream bi = ใหม่ byteArrayInputStream (bo.tobyteArray ()); ObjectInputStream OI = ใหม่ ObjectInputStream (BI); return oi.readObject (); } / ** * การอ้างอิงผ่านและสำเนาลึก * @throws classnotFoundException * @throws ioexception * / โมฆะสาธารณะคงที่ demo1 () พ่น classnotfoundexception, ioexception { System.out.println ("============================================================================================================ - - - 1; // เริ่มต้นค่าของ I ใน DC1 DEEPCOPY DC2 = DC1; System.out.println ("================================================================================================= - - - (DEEPCOPY) DEEPCOPY (DC3); โมฆะคงที่ demo2 () พ่น classnotfoundexception, ioexception {system.out.println ("=============================================================================== System. System. Out of System. System. Out of System. System. Out of System. System. Out of System. System. Out of System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. Out of System. System. System. System. Out of System. System. System. System. Out of System. System. System. System. Out of System. System. System. System. Out of System. System. System. System. Out of System. System. System. System. System. Out of System. System. System. System. System. System. Out of System. System. System. System. System. System. Out of System. System. System. System. System. System. System. System. Out of System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System. System src) The result is the same //Change the value of the element in list1 for(DeepCopy d1 : list1) { //Change the value of i in dc1 d1.i = 2; } // รายการการถ่ายโอนสำหรับ (DEEPCOPY D1: List1) {System.out.println ("list1:" + d1.i); System.out.println ("======================================================================================== - - - New Deepcopy (); } สำหรับ (DEEPCOPY D3: LIST3) {System.out.println ("list3:" + d3.i);ผลการดำเนินการข้างต้นมีดังนี้ (วัดจริง):
- - - - : 2List4 (สำเนาลึก): 1
จะเห็นได้ว่าเมื่ออ้างถึงการส่งผ่านหรือสร้างค่าใหม่ตามค่าของวัตถุมันสามารถเรียกได้ว่า "สำเนาตื้น" เท่านั้น เมื่อคุณสมบัติของการเปลี่ยนแปลงวัตถุต้นฉบับคุณสมบัติของวัตถุใหม่ที่สร้างขึ้นตามวิธีการข้างต้นจะเปลี่ยนไปตาม; และหากใช้สำเนาลึกมันเป็นความจริงที่วัตถุใหม่ถูกคัดลอก วัตถุใหม่ไม่มีความสัมพันธ์กับวัตถุดั้งเดิม การเปลี่ยนแปลงคุณสมบัติของวัตถุต้นฉบับจะไม่ส่งผลกระทบต่อวัตถุใหม่เช่นเดียวกับความหมายของการคัดลอก
ดังที่ได้กล่าวไว้ข้างต้นหากมีความไม่เหมาะสมหากคุณสามารถชี้ให้เห็นได้ขอบคุณมาก
สำหรับเนื้อหาที่เกี่ยวข้องกับ Java เพิ่มเติมผู้อ่านที่สนใจในเว็บไซต์นี้สามารถดูหัวข้อ: "โครงสร้างข้อมูล Java และการสอนอัลกอริทึม", "บทสรุปของเคล็ดลับการดำเนินการ Java Dom Node", "บทสรุปของไฟล์ Java และเคล็ดลับการดำเนินการไดเรกทอรี" และ "สรุป
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน