Dart tili bo'yicha qo'llanma 1-bob. Dart tiliga kirish Dart nima. Birinchi dastur




Download 283,65 Kb.
bet22/35
Sana16.05.2024
Hajmi283,65 Kb.
#237313
1   ...   18   19   20   21   22   23   24   25   ...   35
Bog'liq
Dart tili bo\'yicha qo\'llanma 1-bob. Dart tiliga kirish Dart nima

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 283,65 Kb.
1   ...   18   19   20   21   22   23   24   25   ...   35




Download 283,65 Kb.

Bosh sahifa
Aloqalar

    Bosh sahifa



Dart tili bo'yicha qo'llanma 1-bob. Dart tiliga kirish Dart nima. Birinchi dastur

Download 283,65 Kb.