تصف هذه المقالة وظيفة النسخ المتماثل العميق Java والاستخدام. شاركه للرجوع إليه ، على النحو التالي:
مكتوبة من قبل:
ما هو النسخ المتماثل العميق؟ في جافا ، عند إنشاء كائن ، عادة ما يكون لدينا مرجع يشير إلى الكائن. عندما نغير قيمة (خصائص) الكائن من خلال الإشارة إلى المتغير ، يبقى المرجع دون تغيير ، ويغير الذاكرة في الذاكرة ، أي الكائن الذي يشير إليه. بشكل عام ، عندما نقوم بتعيين هذا المرجع إلى متغير مرجعي آخر أو تمريره كمعلمة ، فإننا نمررها فقط ، أي ، نشير إلى الإشارة إلى "نسخ" نسخة إلى متغير مرجعي آخر ، ثم يشير المتغير المرجعي أيضًا إلى نفس الكائن ، ولا يتم إنشاء كائن جديد في الذاكرة. في بعض الحالات ، نحتاج إلى "نسخ" الكائن حقًا ، وإنشاء نسخة من الكائن المعروف ، وليس فقط "نسخ" المرجع ، سواء كانت نسخة احتياطية أو عمليات أخرى.
لذلك ، كيف تحقق ذلك؟
دعنا نتحدث عن الفكرة أولاً: قم أولاً بتسلسل الكائن في الدفق ، ثم تخلص منه ، وقراءته من الدفق.
الرمز التالي:
package com.yo.java ؛ import java.io.bytearrayinputStream ؛ import java.io.bytearrayoutputstream ؛ import java.io.ioException ؛ import java.io.objectinputstream ؛ import java.oobjectouttream ؛ import java.serializable ؛ Java.Util.List ؛/** * Java تنفذ نسخة عميقة * Author yo * */public class deepcopy أدوات serializable {int i ؛ / ** * param args * throws ioException * throws classNotFoundException */ public static void main (string [] args) remsnotfoundException ، ioException {demo1 () ؛ Demo2 () ؛ }/** * نسخة عميقة ، يجب أن تنفذ فئة المعلمة الفعلية الواجهة القابلة للتسلسل * param o * return * throws ioException * throws classnotfoundException */كائن ثابت عام deepcopy (object o) يلقي IoException ، classnotfoundException {// serialize first and constrate to the bytearrayOuttream by BytearrayTream ؛ ObjectOutputStream oo = new ObjectOutputStream (bo) ؛ oo.writeObject (O) ؛ // ثم deserialize وقراءة من الدفق ، أي نسخ bytearrayinputstream bi = جديد bytearrayinputstream (bo.tobytearray ()) ؛ ObjectInputStream oi = new ObjectInputStream (Bi) ؛ إرجاع oi.ReadObject () ؛ } / ** * مرجع مرجع ونسخ عميق * throws classnotfoundException * throws ioException * / public static void demo1 () remsnotfoundException ، ioException { System.out.println ("============================================================================================================================================ ===================================================================================================================================================================سف ===================================================================================================================================================================سف ============================================================================================================================================================================= 1 ؛ // تهيئة قيمة I في DC1 DeepCopy DC2 = DC1 ؛ System.out.println ("=================================================================================== ==================================================================================================================================== ================================================================================================================================= ==================================================================================================================================== (DeepCopy) DeepCopy (DC3) ؛ static void demo2() throws 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. 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; } // قائمة Transip لـ (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 وبرنامج تعليمي الخوارزمية" ، "ملخص Tips Java Operation DOM" ، "ملخص ملف Java ونصائح تشغيل الدليل" و "ملخص نصائح Java Cache Operation Tips"
آمل أن يكون هذا المقال مفيدًا لبرمجة Java للجميع.