Ienumerable va ienumerator interfeysi metodlarini sinfda qayta aniqlash


Ob’ektlarni nusxa olish. ICloneable interfeysi


Download 31.88 Kb.
bet3/3
Sana16.10.2023
Hajmi31.88 Kb.
#1704713
1   2   3
Bog'liq
IEnumerable va IEnumerator interfeysi metodlarini sinfda qayta aniqlash

Ob’ektlarni nusxa olish. ICloneable interfeysi
Oxirgi yangilash: 31.10.2015

Sinflar bog‘lovchi turlarni taqdim etayotgani uchun, ulardan foydalanishga ayrim cheklovlar qo‘yadi. Xususan:


class Program


{
static void Main(string[] args)
{
Person p1 = new Person { Name="Tom", Age = 23 };
Person p2 = p1;
p2.Name = "Alice";
Console.WriteLine(p1.Name); // Alice

Console.Read();


}
}

class Person


{
public string Name { get; set; }
public int Age { get; set; }
}
Ushbu holda p1 va p2 ob’ektlari ham bir xil ob’ektni ro‘yxatda ko‘rsatadi, shuning uchun p2 o‘zgaruvchisidagi xossalarning o‘zgartirilishi p1 o‘zgaruvchisiga ham ta’sir ko‘rsatadi.

P2 o‘zgaruvchisi yangi ob’ektni ko‘rsatishi uchun, biroq p1 qiymatlari bilan, IKloneable interfeysini amalga oshirish orqali klonlashni amalga oshirishimiz mumkin:


public interface ICloneable
{
object Clone();
}
Person sinfida interfeysni amalga oshirish quyidagicha koʻrinishi mumkin:
class Person : ICloneable
{
public string Name { get; set; }
public int Age { get; set; }
public object Clone()
{
return new Person { Name = this. Name, Age = this. Age };
}
}
Foydalanish:
class Program
{
static void Main(string[] args)
{
Person p1 = new Person { Name="Tom", Age = 23 };
Person p2 = (Person)p1.Clone();
p2.Name = "Alice";
Console.WriteLine(p1.Name); // Tom

Console.Read();


}
}
Endilikda hamma nusxa olinmoqda, p2 xossalaridagi o‘zgarishlar p1 xossalariga ta’sir ko‘rsatmaydi.

Nusxa olish kodini qisqartirish uchun ob’ektning nusxasini qaytaradigan maxsus MemberwiseClone () usulidan foydalanishimiz mumkin:


class Person : ICloneable
{
public string Name { get; set; }
public int Age { get; set; }
public object Clone()
{
return this. MemberwiseClone();
}
}
Ushbu usul yuqori (chuqur bo‘lmagan) nusxa olishni amalga oshiradi. Biroq ushbu nusxa olish etarli bo‘lmasligi mumkin. Masalan, Person sinfida Company:
class Person : ICloneable
{
public string Name { get; set; }
public int Age { get; set; }
public Company Work { get; set; }

public object Clone()


{
return this. MemberwiseClone();
}
}

class Company


{
public string Name { get; set; }
}
Ushbu holda nusxa olishda yangi nusxa ham shunday Company ob’ektini ko‘rsatadi:
Person p1 = new Person { Name="Tom", Age = 23, Work= new Company { Name = "Microsoft" } };
Person p2 = (Person)p1.Clone();
p2.Work.Name = "Google";
p2.Name = "Alice";
Console.WriteLine(p1.Name); // Tom
Console.WriteLine (p1.Work.Name) ;//Google - a
Sirtdan nusxa olish faqatgina ibtidoiy turlarni ifodalovchi xossalar uchun ishlaydi, lekin murakkab obʼyektlar uchun emas. Bunda quyidagilar chuqur nusxa olinishi kerak:
class Person : ICloneable
{
public string Name { get; set; }
public int Age { get; set; }
public Company Work { get; set; }

public object Clone()


{
Company company = new Company { Name = this. Work.Name };
return new Person
{
Name = this. Name,
Age = this. Age,
Work = company
};
}
}

class Company


{
public string Name { get; set; }
}
Download 31.88 Kb.

Do'stlaringiz bilan baham:
1   2   3




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling