6 Umumlashtirishlarda interfeyslar
Interfeyslar umumlashtirishning cheklanishi sifatida
Interfeyslar umumlashtirishning cheklanishi sifatida harakat qilishi mumkin. Bundan tashqari, agar cheklash sifatida faqat bitta klass belgilanishi mumkin bo'lsa, unda bir nechta interfeyslarni ko'rsatish mumkin.
Aytaylik, bizda quyidagi interfeyslar va ularni bajaradigan sinf mavjud:
interface IAccount
{
int CurrentSum { get; } //Xisobdagi hozirgi summa
void Put(int sum); // Hisobga pul qo'ying
void Withdraw(int sum); // Hisobdan oling
}
interface IClient
{
string Name { get; set; }
}
class Client : IAccount, IClient
{
int _sum; // Miqdorni saqlash uchun o'zgaruvchan
public Client(string name, int sum)
{
Name = name;
_sum = sum;
}
public string Name { get; set; }
public int CurrentSum
{
get { return _sum; }
}
public void Put(int sum)
{
_sum += sum;
}
public void Withdraw(int sum)
{
if (sum <= _sum)
{
_sum -= sum;
}
}
}
|
Biz yuqoridagi interfeyslarni umumlashtirilgan sinf uchun cheklov sifatida ishlatamiz:
class Transaction where T: IAccount, IClient
{
public void Operate(T acc1, T acc2, int sum)
{
if(acc1.CurrentSum >= sum)
{
acc1.Withdraw(sum);
acc2.Put(sum);
Console.WriteLine($"{acc1.Name} : {acc1.CurrentSum}\n{acc2.Name} : {acc2.CurrentSum}");
}
}
}
|
Bunday holda, T parametri bir vaqtning o'zida ikkita IAccount va IClient
Client account1 = new Client("Tom", 200);
Client account2 = new Client("Bob", 300);
Transaction transaction = new Transaction();
transaction.Operate(account1, account2, 150);
|
interfeyslarini amalga oshiradigan turni anglatadi. Masalan, har ikkala interfeysni ham amalga oshiradigan Client sinfi yuqorida aniqlangan, shuning uchun biz Transaction ob'ektlarini ushbu tur bilan yoza olamiz:
Shuningdek, T parametri ikkala interfeysdan meros bo'lib qolgan interfeysni ifodalashi mumkin:
interface IClientAccount : IAccount, IClient
{
}
class ClientAccount : IClientAccount
{
int _sum;
public ClientAccount(string name, int sum)
{
_sum = sum; Name = name;
}
public int CurrentSum { get { return _sum; } }
public string Name { get; set; }
public void Put(int sum)
{
_sum += sum;
}
public void Withdraw(int sum)
{
if (_sum >= sum) _sum -= sum;
}
}
|
Bunday holda biz Transaction ob'ektlarini IClientAccount turi bilan terishimiz mumkin:
IClientAccount account3 = new ClientAccount("Alice", 400);
IClientAccount account4 = new ClientAccount("Kate", 500);
Transaction operation = new Transaction();
operation.Operate(account3, account4, 200);
|
interface IUser
{
T Id { get; }
}
class User : IUser
{
T _id;
public User(T id)
{
_id = id;
}
public T Id { get { return _id; } }
}
| Umumiy interfeyslar.Sinflar singari interfeyslarni ham umumlashtirish mumkin:
IUser interfeysi interfeysni amalga oshirishda foydalanuvchi sinfida ishlatiladigan T parametri bilan yoziladi. Xususan, _id o'zgaruvchisi T sifatida aniqlanadi, bu esa id uchun har xil turlardan foydalanishga imkon beradi.
Ikkita bajarishni aniqlaymiz: biri parametr sifatida int turini, ikkinchisi esa satr turini ishlatadi:
IUser user1 = new User(6789);
Console.WriteLine(user1.Id); // 6789
IUser user2 = new User("12345");
Console.WriteLine(user2.Id); // 12345
|
Bundan tashqari, interfeysni amalga oshirishda biz T parametriga qaysi tur ishlatilishini aniq ko'rsatamiz:
class IntUser : IUser
{
int _id;
public IntUser(int id)
{
_id = id;
}
public int Id { get { return _id; } }
}
|
Do'stlaringiz bilan baham: |