Dart tiliga kirish O'zgaruvchilar va konstantalar Primitiv ma'lumotlar turlari


Download 374.15 Kb.
bet22/35
Sana11.05.2023
Hajmi374.15 Kb.
#1454719
1   ...   18   19   20   21   22   23   24   25   ...   35
Bog'liq
3-mustaqil ish

Aralashmalar

Miksinlar Dartda ko'p meros olish muammosining yana bir yechimidir. Aslini olganda, miksin oddiy sinfni ifodalaydi, bundan mustasno, unda konstruktor bo'lmasligi kerak, aks holda uni miksin sifatida ishlatib bo'lmaydi. With iborasi mikslarni qo'llash uchun ishlatiladi:




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

void main (){
Employee bob = Employee("Bob", "Google");
bob.display(); // Name: Bob
bob.work(); // Works in Google
}
class Person{
String name;
Person(this.name);
void display(){
print("Name: $name");
}
}
class Worker{
String company = "";
void work(){
print("Work in $company");
}
}
class Employee extends Person with Worker{
Employee(name, comp) : super(name){
company = comp; // обращаемся к полю company, определенному в миксине Worker
}
}

Bunda Ishchi sinfi miksin vazifasini bajaradi. Uning funksionalligidan foydalanish uchun, Employee sinfini belgilashda with iborasi ishlatiladi:




1

class Employee extends Person with Worker{

Keyin Xodimlar sinfi Ishchi sinfidagi maydonlar va usullardan foydalanishi mumkin. Biroq, Employee sinfi boshqa sinflardan meros bo'lishi mumkin, chunki bu holatda Person sinfidan.


Biroq, Ishchi turi oddiy sinf sifatida belgilanganligi sababli, biz uni mustaqil ravishda ishlatishimiz mumkin:




1
2
3

Worker tom = Worker();
tom.company = "Apple";
tom.work();

Shu bilan birga, biz ushbu tur faqat miksin sifatida ishlatilishini ko'rsatishimiz mumkin (agar biz uni alohida sinf sifatida ishlatishni rejalashtirmasak). Bunday holda, turni aniqlash uchun mixin kalit so'zi ishlatiladi:




1
2
3
4
5
6

mixin Worker{
String company = "";
void work(){
print("Work in $company");
}
}



Download 374.15 Kb.

Do'stlaringiz bilan baham:
1   ...   18   19   20   21   22   23   24   25   ...   35




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