Thuta Learning
Dart
IntermediateProgrammingအခြေခံ

Classes & Objects

Dartသင်ခန်းစာ 1435 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Field နဲ့ method ပါတဲ့ class တစ်ခု သတ်မှတ်ပြီး အဲဒီကနေ object တွေ ဖန်တီးနိုင်ဖို့
  • Instance member နဲ့ static member ကို ခွဲခြားပြီး ဘယ်အချိန် ဘာသုံးရမလဲ ပြောနိုင်ဖို့
  • Getter နဲ့ setter ကို တွက်ချက်ဖို့၊ validate လုပ်ဖို့ သုံးပြီး Dart မှာ _name ရဲ့ privacy က တကယ်ဘာလဲ ရှင်းပြနိုင်ဖို့
  • toString, == နဲ့ hashCode ကို override လုပ်ပြီး object တွေ အဓိပ္ပာယ်ရှိရှိ print ထုတ်၊ နှိုင်းယှဉ်နိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

ရိုးရိုးလေး ရှင်းရရင်

Class ဆိုတာ အိမ်ဆောက်ဖို့ ပုံစံ (blueprint) လိုပါပဲ။ အိမ်တိုင်းမှာ အခန်းအရေအတွက်၊ အရောင်လို အချက်အလက်တွေ (field) နဲ့ တံခါးဖွင့်တာလို လုပ်ဆောင်ချက်တွေ (method) ပါတယ်လို့ ပုံစံက ဖော်ပြပါတယ်။ အဲဒီပုံစံကနေ ဆောက်ထားတဲ့ အိမ်တစ်လုံးချင်းစီက object ပါ၊ အိမ်တစ်လုံးစီမှာ ကိုယ်ပိုင် အရောင်၊ ကိုယ်ပိုင် အခန်းအရေအတွက် ရှိပေမယ့် ပုံစံတူတူပါပဲ။

Dart မှာ number, String, List တွေ အားလုံးက object တွေပါပဲ။ ကိုယ်ပိုင် class ရေးတာဟာ "Course"၊ "User"၊ "Order" လို ကိုယ့် app ရဲ့ စကားလုံးတွေကို language ထဲ ထည့်ပေးတာပါ။ ပြီးရင် code က map ရဲ့ key စာသားတွေ မဟုတ်တော့ဘဲ course.title လို တိတိကျကျ ဖတ်လို့ရတဲ့ ပုံစံ ဖြစ်လာပါတယ်။

Problem → Why → Solution

ပြဿနာ: Map နဲ့ data ကို ကိုင်ရင် ဘာမှ မစစ်ပေးဘူး

သင်က learning app တစ်ခုအတွက် course တွေကို model လုပ်နေတယ် ဆိုပါစို့။ လွယ်တဲ့ နည်းက Map ပါ: {'title': 'Dart Basics', 'lessons': 18}။ ဒုတိယ course ကျမှ key တစ်ခုမှာ စာလုံးပေါင်းမှားပြီး ဂဏန်းကို စာသားအဖြစ် သိမ်းလာတဲ့အခါထိ အဆင်ပြေပါတယ်။ Dart က နှစ်ခုလုံးကို တိတ်တိတ်လေး လက်ခံပါတယ်: 'title' ကို ဖတ်ရင် null ကို တိတ်တိတ်ပေးပြီး lessons ကို 24 နဲ့ နှိုင်းယှဉ်ရင် false ကို တိတ်တိတ်ပေးပါတယ်။ Key တွေအတွက် autocomplete မရှိ၊ ဘယ် key တွေ မဖြစ်မနေ ရှိရမလဲ ဘယ်သူမှ မပြော၊ "ဒီ course ကို publish လုပ်ပါ" လို လုပ်ဆောင်ချက်ကိုလည်း map က မသယ်နိုင်ပါဘူး။

လိုချင်တာက field တွေ သတ်မှတ်ထားပြီး type တွေကို compiler စစ်ပေးတဲ့ နာမည်ပါ ပုံသဏ္ဌာန်တစ်ခုနဲ့ အဲဒီပုံသဏ္ဌာန်နဲ့ ပိုင်ဆိုင်တဲ့ function တွေပါ။ ဒါက class က ပေးတာ အတိအကျပါပဲ။

map_course.dart
void main() {
  final course = {'title': 'Dart Basics', 'lessons': 18};
  final other = {'titel': 'Flutter', 'lessons': '24'}; // typo, and the number is text

  print(course['lessons']);
  print(other['title']);         // null: nothing warns you
  print(other['lessons'] == 24); // false: it is the text '24'
}

Class က ဘာကို ပြောင်းပေးလဲ

Class က "course map တိုင်းမှာ title နဲ့ lessons ရှိတယ်" ဆိုတဲ့ အလွတ်သဘောတူညီချက်ကို compiler က တကယ်ကျင့်သုံးခိုင်းတဲ့ အရာ ဖြစ်စေပါတယ်။ Field နာမည် စာလုံးပေါင်းမှားရင် တိတ်တိတ်လေး null ပေါ်တာ မဟုတ်ဘဲ compile error ပေါ်ပြီး field type တွေက ဂဏန်းကို စာသားအဖြစ် သိမ်းတာကို တားပါတယ်။

Core concept: class၊ instance နဲ့ static member၊ getter နဲ့ privacy

Class က blueprint ဖြစ်ပြီး object ကတော့ အဲဒီကနေ ဆောက်ထားတဲ့ အရာတစ်ခုပါ။ Field တွေက data (String title) ကို ကိုင်ပြီး method တွေက အပြုအမူ (void publish()) ကို ကိုင်ပါတယ်၊ object တစ်ခုစီမှာ field တိုင်းရဲ့ ကိုယ်ပိုင်မိတ္တူ ရှိလို့ course တစ်ခုရဲ့ lessons ကို ပြောင်းရင် တခြား course ကို မထိပါဘူး။ Constructor ကို ခေါ်ပြီး object ဖန်တီးပါတယ်၊ Course('Dart Basics', 18)၊ Dart မှာ new keyword မလိုပါဘူး၊ style guide တွေကလည်း မရေးဖို့ ပြောပါတယ်။

text
One class, two objects, one shared counter
------------------------------------------
class Course              objects (own copy of each field)
+---------------+         basics  --> title: 'Dart Basics'  lessons: 18
| title         |         flutter --> title: 'Flutter'      lessons: 0
| lessons       |
| publish()     |         Course.created --> 2   (one value, shared)
+---------------+

Member တွေက မူလအားဖြင့် instance member တွေပါ — object တစ်ခုချင်းစီနဲ့ သက်ဆိုင်ပြီး အဲဒီအထဲမှာ ကိုယ့် object ရဲ့ တခြား field နဲ့ method တွေကို နာမည်နဲ့ တန်း ဖတ်ပါတယ် (this. က မလိုပါဘူး)။ Static member ကတော့ class ကိုယ်တိုင်နဲ့ သက်ဆိုင်ပါတယ်: static int created က course အားလုံး မျှဝေသုံးတဲ့ variable တစ်ခုတည်းဖြစ်ပြီး static String shortTitle(...) က object မလိုတဲ့ function ပါ။ Static member တွေကို class နာမည် Course.created နဲ့ ယူရပါတယ်၊ instance ကနေ မရပါဘူး။ Static method က instance field တွေကို မဖတ်နိုင်ပါဘူး၊ ဖတ်စရာ object မရှိလို့ပါ။ Counter, constant (static const) နဲ့ object တစ်ခုရဲ့ data ပေါ် မမူတည်တဲ့ helper တွေအတွက် static ကို သုံးပါ။

Getter က ဖတ်တဲ့အခါ field လို ပုံပေါ်ပေမယ့် code run ပါတယ်: String get summary => '$title ($lessons lessons)' က အကြိမ်တိုင်း စာသားကို တွက်ပါတယ်။ Setter, set age(int value) က assignment လို ပုံပေါ်ပြီး validate လုပ်နိုင်ပါတယ်။ ရိုးရိုး field တွေက getter နဲ့ setter အလုပ်ကို ဒီအတိုင်း လုပ်ပြီးသားမို့ ရိုးရိုး field နဲ့ စပြီး နောက်မှ getter ဒါမှမဟုတ် setter ပြောင်းလို့ ရပါတယ်၊ ခေါ်တဲ့နေရာ တစ်ခုမှ ပြင်စရာ မလိုပါဘူး။ final field မှာ getter ပဲ ရှိလို့ assign လုပ်ရင် compile error ပါ။

Dart မှာ private ဒါမှမဟုတ် public keyword မရှိပါဘူး။ _published လို underscore နဲ့ စတဲ့ နာမည်က သူ့ library အတွင်းမှာ private ဖြစ်ပြီး library ဆိုတာ များသောအားဖြင့် class တစ်ခု မဟုတ်ဘဲ file တစ်ခုပါ။ အဲဒီ file ထဲက တခြား class ဒါမှမဟုတ် function ဘယ်ဟာမဆို _published ကို ဖတ်၊ ရေးနိုင်ပါတယ်။ Field ကို တကယ် ဖျောက်ချင်ရင် class ကို သူ့ file တစ်ခုတည်းမှာ ထားပြီး တခြား file တွေကနေ import လုပ်ပါ၊ ဒါဆို တခြား file တွေက နာမည်ကိုတောင် မမြင်ရပါဘူး။

မှတ်ထားရမယ့် ပုံစံ: ပုံစံတစ်ခု၊ ဆောက်ထားတဲ့ အရာများ
  1. Class: ပုံစံ (blueprint)
  2. Object: အဲဒီကနေ ဆောက်ထားတဲ့ အရာတစ်ခု
  3. Instance member: object တစ်ခုစီမှာ ကိုယ်ပိုင်
  4. static member: class တစ်ခုလုံးမှာ တစ်ခုတည်း

Member တိုင်းအတွက် မေးပါ — ဒါက object တစ်ခုနဲ့ သက်ဆိုင်လား၊ class တစ်ခုလုံးနဲ့ သက်ဆိုင်လား။

အထွေထွေ ပုံစံ

dart
class Name {
  static int shared = 0;          // one copy for the whole class
  final String id;                // each object has its own copy
  int count = 0;
  Name(this.id);                  // constructor
  int get doubled => count * 2;   // computed property
  void action() { /* behaviour */ }
  @override
  String toString() => 'Name($id)';
}

class ရဲ့ body ထဲမှာ field, constructor, getter / setter နဲ့ method တွေ ရှိပါတယ်။ static နဲ့ မစတဲ့ member တိုင်းက instance member ပါ၊ @override ကတော့ Object ကနေ ပါလာတဲ့ toString လို member ကို ကိုယ့်ဟာကိုယ် အစားထိုးတာကို ဖော်ပြတာပါ။

Example: Course class

ဒီ Course class မှာ အရေးကြီးတဲ့ အပိုင်းတိုင်း ပါပါတယ်: final field တစ်ခု၊ ပြောင်းလို့ရတဲ့ field တစ်ခု၊ underscore နဲ့ ဖျောက်ထားတဲ့ field တစ်ခု၊ getter နှစ်ခု၊ method တစ်ခု၊ static counter နဲ့ static helper ကို toString တွဲပါတယ်။ main() က course နှစ်ခု ဖန်တီးပြီး တစ်ခုက lessons မရှိလို့ publish မလုပ်နိုင်တာကို ပြပါတယ်။ Run မလုပ်ခင် Course.created ဘာ ဖြစ်မလဲ ခန့်မှန်းကြည့်ပါ။

course.dart
class Course {
  static int created = 0;

  final String title;
  int lessons;
  bool _published = false;

  Course(this.title, this.lessons) {
    created++;
  }

  bool get isPublished => _published;

  String get summary => '$title ($lessons lessons)';

  void publish() {
    if (lessons == 0) {
      print('cannot publish an empty course');
      return;
    }
    _published = true;
  }

  static String shortTitle(String text) =>
      text.length > 8 ? '${text.substring(0, 8)}...' : text;

  @override
  String toString() => 'Course($title, $lessons)';
}

void main() {
  final basics = Course('Dart Basics', 18);
  final flutter = Course('Flutter', 0);

  print(basics);
  print(basics.summary);
  print(basics.isPublished);
  basics.publish();
  flutter.publish();
  print(basics.isPublished);

  basics.lessons = 20;
  print(basics.summary);

  print(Course.created);
  print(Course.shortTitle(basics.title));
}

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Shared counterstatic int created = 0;

    Static variable တစ်ခုတည်းကို course အားလုံး မျှဝေသုံးပါတယ်။ Constructor ထဲက created++ က object တစ်ခု ဖန်တီးတိုင်း တိုးပေးလို့ course နှစ်ခုပြီးရင် 2 ဖြစ်ပါတယ်၊ Course.created နဲ့ ဖတ်ပါတယ်။

  2. final vs mutablefinal String title;

    final field ကို constructor မှာ တစ်ကြိမ်ပဲ သတ်မှတ်လို့ရပြီး setter မရှိပါဘူး၊ basics.title = 'x' လို့ ရေးရင် compile error ပါ။ ဘေးက int lessons ကတော့ ရိုးရိုး field မို့ basics.lessons = 20 လို့ ပြောင်းလို့ရပါတယ်။

  3. Library-private fieldbool _published = false;

    Underscore ကြောင့် _published က ဒီ file အတွင်းမှာပဲ မြင်ရပါတယ်။ ပြင်ပ code က တိုက်ရိုက် မပြောင်းနိုင်ဘဲ publish() ကိုပဲ ခေါ်ရတာမို့ "lessons မရှိရင် publish မလုပ်ရ" ဆိုတဲ့ စည်းမျဉ်းကို class ကိုယ်တိုင် ထိန်းနိုင်ပါတယ်။

  4. Read-only getterbool get isPublished => _published;

    ပြင်ပကို ဖတ်ခွင့်ပဲ ပေးတဲ့ property တစ်ခုပါ။ Setter မရှိလို့ basics.isPublished = true လို့ ရေးရင် compile error ပါ။ ခေါ်တဲ့အခါ () မထည့်ဘဲ field လို basics.isPublished လို့ ရေးပါတယ်။

  5. Guard in a methodif (lessons == 0) {

    publish() က state မပြောင်းခင် စစ်ပါတယ်။ lessons မရှိတဲ့ flutter course အတွက် message ပြပြီး return နဲ့ ထွက်လို့ _published က false အတိုင်း ကျန်ပါတယ်။

  6. Static helperstatic String shortTitle(String text) =>

    Object မလိုတဲ့ function မို့ Course.shortTitle(...) နဲ့ ခေါ်ပါတယ်။ this မရှိလို့ title, lessons လို instance field တွေကို ဒီအထဲက မဖတ်နိုင်ဘဲ parameter ကိုပဲ သုံးရပါတယ်။

  7. toStringString toString() => 'Course($title, $lessons)';

    print(basics) နဲ့ string interpolation က toString() ကို ခေါ်ပါတယ်။ Override မလုပ်ရင် "Instance of 'Course'" လို အသုံးမဝင်တဲ့ စာသား ပေါ်ပါတယ်။

You should see
Course(Dart Basics, 18)
Dart Basics (18 lessons)
false
cannot publish an empty course
true
Dart Basics (20 lessons)
2
Dart Bas...

Object တွေကို နှိုင်းယှဉ်နိုင်အောင်: toString, == နဲ့ hashCode

Class တစ်ခုက Object ကနေ အပြုအမူ သုံးခုကို အလိုအလျောက် ရပါတယ်: toString() က "Instance of 'Money'" ပေးတယ်၊ == က identity (object တူညီတာ) ကိုပဲ စစ်တယ်၊ hashCode က identity ကို အခြေခံတယ်။ ဒါကြောင့် Money(5, 'USD') နှစ်ခုက တန်ဖိုးတူပေမယ့် == နဲ့ မညီပါဘူး၊ Set ထဲမှာလည်း နှစ်ခု ဖြစ်နေပါတယ်။ တန်ဖိုးကိုက်တာကို အရေးကြီးတဲ့ class (Point, Money, Email) တွေမှာ == နဲ့ hashCode ကို အတူ override လုပ်ပါ၊ hashCode ကို == စစ်တဲ့ field တွေကိုပဲ Object.hash(x, y) နဲ့ တည်ဆောက်ပါ။ identical(a, b) ကတော့ object တူညီမှု စစ်ဖို့ အမြဲ ရှိနေပါတယ်။

equality.dart
class Money {
  final int amount;
  final String currency;
  Money(this.amount, this.currency);
}

class Point {
  final int x, y;
  Point(this.x, this.y);

  @override
  bool operator ==(Object other) =>
      other is Point && other.x == x && other.y == y;

  @override
  int get hashCode => Object.hash(x, y);

  @override
  String toString() => 'Point($x, $y)';
}

void main() {
  final m1 = Money(5, 'USD');
  final m2 = Money(5, 'USD');
  print(m1);
  print(m1 == m2);
  print({m1, m2}.length);

  final p1 = Point(1, 2);
  final p2 = Point(1, 2);
  print(p1);
  print(p1 == p2);
  print(identical(p1, p2));
  print({p1, p2}.length);
}
Memberမူလ အပြုအမူOverride လုပ်သင့်တဲ့အချိန်
toString()"Instance of 'ClassName'"Debug, log နဲ့ UI မှာ ဖတ်လို့ရတဲ့ စာသား လိုတဲ့အခါ
== operatorIdentity (object တူညီမှု)တန်ဖိုးတူရင် တူတယ်လို့ သတ်မှတ်ချင်တဲ့ class
hashCodeIdentity အခြေခံ== ကို override လုပ်တိုင်း၊ field တူတူနဲ့ တွဲပါ

Library privacy နဲ့ setter validation လက်တွေ့

ဒီ Account class က _balance ကို underscore နဲ့ ဖျောက်ထားပြီး getter နဲ့ deposit() ကနေပဲ ပြောင်းလို့ရအောင် လုပ်ထားပါတယ်။ ဒါပေမယ့် တူညီတဲ့ file ထဲက audit() function က a._balance ကို တိုက်ရိုက် ပြောင်းနိုင်ပါတယ် — underscore က class-level private မဟုတ်ဘဲ library-level private ဖြစ်လို့ပါ။ ဒါကို မသိဘဲ "class ထဲမှာ ပိတ်ထားပြီး" လို့ ထင်ရင် အံ့ဩစရာ ကြုံပါလိမ့်မယ်။ Setter ကတော့ ကြိုတင် validate လုပ်ဖို့ သင့်တော်ပါတယ်: limit = -5 လို မမှန်တဲ့ တန်ဖိုးကို ပစ်ပယ်ပါတယ်။ Setter ကို getter တွဲမပါဘဲ ရေးနိုင်ပေမယ့် ဒီမှာ getter ပါ တွဲပေးထားတာကို သတိပြုပါ — ဒါမှ limit ကို ဖတ်လို့ရပါမယ်။

account.dart
class Account {
  double _balance = 0;
  double _limit = 100;

  double get balance => _balance;
  double get limit => _limit;

  set limit(double value) {
    if (value < 0) {
      throw ArgumentError('limit must not be negative');
    }
    _limit = value;
  }

  void deposit(double amount) {
    if (amount <= 0) {
      throw ArgumentError('deposit must be positive');
    }
    _balance += amount;
  }
}

void audit(Account account) {
  // Same file, so same library: the underscore does not stop this.
  account._balance = 1000000;
}

void main() {
  final account = Account();
  account.deposit(50);
  print(account.balance);

  audit(account);
  print(account.balance);

  account.limit = 250;
  print(account.limit);
  try {
    account.limit = -5;
  } catch (e) {
    print('caught: $e');
  }
}

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Dart app အားလုံးနီးပါးက class တွေရဲ့ စုစည်းမှုပါ — JSON ကနေ parse လုပ်ထားတဲ့ data ကိုင်တဲ့ User, Product, Order model တွေ၊ server နဲ့ ဆက်သွယ်တဲ့ service တွေ၊ Flutter မှာဆိုရင် widget တိုင်းက class ပါ။ total, isEmpty လို တွက်ထုတ်တဲ့ တန်ဖိုးတွေကို getter နဲ့ တစ်နေရာတည်း ထားပါတယ် — List class ကိုယ်တိုင်မှာတောင် isEmpty, length, first တွေက getter တွေပါ။ Static member တွေက static const maxRetries = 3 လို constant နဲ့ counter တွေ ကိုင်ပါတယ်။

Money ဒါမှမဟုတ် Email လို တန်ဖိုးအမျိုးအစား class တွေက == နဲ့ hashCode ကို override လုပ်လို့ တန်ဖိုးတူတဲ့ object နှစ်ခုက Set ဒါမှမဟုတ် Map key အဖြစ် တူညီတဲ့အရာလို အလုပ်လုပ်ပါတယ်။ toString ကတော့ debug လုပ်တဲ့ နေရာတိုင်းမှာ ပေါ်ပါတယ်၊ print(object) နဲ့ string interpolation က ခေါ်ပေးလို့ပါ။ Class ရဲ့ field တွေကို final ထားပြီး state ပြောင်းတာကို method အနည်းငယ်နောက်မှာ ထားရင် တွေးရလွယ်ပါတယ် — ဒါက ပရော်ဖက်ရှင်နယ် Dart code တွေ လိုက်နာတဲ့ အကျင့်ပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Underscore က တခြား class ကနေ ဖျောက်ပေးတယ်လို့ ထင်တာ

_balance က Account class တစ်ခုတည်းမှာ private မဟုတ်ပါဘူး၊ file တစ်ခုလုံး (library) မှာ private ပါ။ တူညီတဲ့ file ထဲက တခြား class နဲ့ function တွေက ဖတ်၊ ရေးနိုင်ပါတယ်။ တကယ် ကာကွယ်ချင်တဲ့ class ကို သူ့ file ထဲ သီးသန့်ထားပြီး import လုပ်ပါ။

2. hashCode မပါဘဲ == ကို override လုပ်တာ

== ကိုပဲ override လုပ်ပြီး hashCode မလုပ်ရင် တန်ဖိုးတူတဲ့ object နှစ်ခုက hash ကွဲလို့ Set ထဲမှာ နှစ်ခုစလုံး ကျန်နိုင်ပြီး Map key အနေနဲ့ ရှာရင် null ပြန်ပါတယ်။ == စစ်တဲ့ field တွေ အတူတူနဲ့ Object.hash(x, y) လို့ ရေးပြီး နှစ်ခုကို အမြဲ တွဲပြင်ပါ။

3. Static ကို instance ကနေ၊ instance ကို class နာမည်နဲ့ ယူတာ

c.created လို့ instance ကနေ ယူရင် "The getter 'created' isn't defined for the type 'Course'." ဆိုတဲ့ error ပေါ်ပြီး Course.x လို့ instance field ကို class နာမည်နဲ့ ယူရင် "Member not found: 'x'." ပေါ်ပါတယ်။ Static ကို Course.created ဆိုပြီး class နာမည်နဲ့၊ instance member ကို object နဲ့ ယူပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • ပြောင်းစရာမလိုတဲ့ field တွေကို final လုပ်ပါ၊ ပြောင်းလဲမှုတွေကို method အနည်းငယ်နောက်မှာပဲ ထားပါ။
  • တွက်ထုတ်တဲ့ တန်ဖိုးတွေကို (total, isEmpty) သီးသန့် field အဖြစ် သိမ်းမနေဘဲ getter အဖြစ် ဖော်ပြပါ၊ ဒါဆို တခြား field နဲ့ မကိုက်ညီတဲ့ အခြေအနေ မဖြစ်ပါဘူး။
  • Class တိုင်းမှာ toString ကို override လုပ်ပါ၊ debug လုပ်ရတာ အများကြီး လွယ်လာပါတယ်။
  • == နဲ့ hashCode ကို အတူ override လုပ်ပြီး field တူတူကို နှစ်ခုလုံးမှာ သုံးပါ။

Mini challenge

Mini challenge: ဈေးဝယ်လှည်း (Cart)

Product နဲ့ Cart class နှစ်ခု ရေးပါ။ Product က နာမည်နဲ့ ဈေးတူရင် တူညီတယ်လို့ သတ်မှတ်ပြီး Cart ကတော့ ပစ္စည်း ၃ ခုထက် မပိုရအောင်၊ တူညီတဲ့ ပစ္စည်း ထပ်မထည့်နိုင်အောင် ထိန်းပါ။

  • Product မှာ final name နဲ့ price (int) ရှိပြီး ==, hashCode နဲ့ toString (ဥပမာ 'pen (2)') ကို override လုပ်ပါ
  • Cart မှာ static const maxItems = 3 နဲ့ underscore ပါတဲ့ private list ရှိပါစေ
  • bool add(Product product) က ပြည့်နေရင် ဒါမှမဟုတ် ပစ္စည်း ရှိပြီးသားဆို false ပြန်ပြီး မဟုတ်ရင် ထည့်ပြီး true ပြန်ပါ
  • getter itemCount နဲ့ total ကို ရေးပြီး Cart ရဲ့ toString က 'Cart(pen (2), book (12))' ပုံစံ ထုတ်ပါ
  • main() မှာ pen(2), book(12), pen(2) ထပ်၊ lamp(30), desk(90) ကို ထည့်ပြီး add() ရလဒ်တိုင်း၊ cart၊ နောက်ဆုံး items နဲ့ total ကို print ထုတ်ပါ
cart.dart
class Product {
  // TODO: final name and price, set through the constructor
  // TODO: override ==, hashCode and toString
}

class Cart {
  // TODO: static const maxItems = 3
  // TODO: a private list of products
  // TODO: bool add(Product product): false when full or already present
  // TODO: getters itemCount and total
  // TODO: toString such as Cart(pen (2), book (12))
}

void main() {
  // TODO: add pen(2), book(12), pen(2) again, lamp(30), desk(90) and print
  //       each add() result, then the cart, then "items: 3, total: 44"
}

ရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: true true false true false Cart(pen (2), book (12), lamp (30)) items: 3, total: 44

အရိပ်အမြွက် ကြည့်ရန်

Product ရဲ့ == မှာ other is Product && other.name == name && other.price == price လို့ ရေးပြီး hashCode ကို Object.hash(name, price) နဲ့ ရေးပါ။ Cart.add မှာ _items.contains(product) က == ကို သုံးလို့ တူညီတဲ့ ပစ္စည်းကို ရှာတွေ့ပါလိမ့်မယ်။ _items.join(', ') က toString အတွက် အဆင်ပြေပါတယ်။

အဖြေ ကြည့်ရန်
cart.dart
class Product {
  final String name;
  final int price;

  Product(this.name, this.price);

  @override
  bool operator ==(Object other) =>
      other is Product && other.name == name && other.price == price;

  @override
  int get hashCode => Object.hash(name, price);

  @override
  String toString() => '$name ($price)';
}

class Cart {
  static const maxItems = 3;

  final List<Product> _items = [];

  bool add(Product product) {
    if (_items.length >= maxItems || _items.contains(product)) {
      return false;
    }
    _items.add(product);
    return true;
  }

  int get itemCount => _items.length;

  int get total {
    var sum = 0;
    for (final item in _items) {
      sum += item.price;
    }
    return sum;
  }

  @override
  String toString() => 'Cart(${_items.join(', ')})';
}

void main() {
  final cart = Cart();
  print(cart.add(Product('pen', 2)));
  print(cart.add(Product('book', 12)));
  print(cart.add(Product('pen', 2)));
  print(cart.add(Product('lamp', 30)));
  print(cart.add(Product('desk', 90)));
  print(cart);
  print('items: ${cart.itemCount}, total: ${cart.total}');
}

Quick Quiz 1 / 3

Class A နဲ့ class B က file တစ်ခုတည်းမှာ ရှိပါတယ်။ B ရဲ့ method က A ရဲ့ underscore field ဖြစ်တဲ့ a._secret ကို ဖတ်ရင် ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

class Counter { static int total = 0; int count = 0; } ကို အခြေခံရင် ဘယ် statement က မှန်လဲ။

Quick Quiz 3 / 3

Class တစ်ခုက == ကိုပဲ override လုပ်ပြီး hashCode ကို မလုပ်ပါဘူး။ အဓိက အန္တရာယ်က ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Class က field နဲ့ method ပါတဲ့ blueprint ဖြစ်ပြီး object တစ်ခုစီမှာ field တိုင်းရဲ့ ကိုယ်ပိုင်မိတ္တူ ရှိပါတယ်။
  • static member တွေက class တစ်ခုလုံးအတွက် တစ်ခုတည်းဖြစ်ပြီး class နာမည်နဲ့ ယူရပါတယ်၊ instance member တွေကို object ကနေ ယူရပါတယ်။
  • Getter နဲ့ setter က field လို ပုံပေါ်ပေမယ့် code run ပါတယ်၊ underscore ကတော့ class မဟုတ်ဘဲ library (file) အတွင်း private ဖြစ်စေပါတယ်။
  • toString ကို override လုပ်ပြီး == နဲ့ hashCode ကို အတူ override လုပ်မှ object တွေ မျှော်လင့်တဲ့အတိုင်း print ထွက်၊ နှိုင်းယှဉ်လို့ရပါတယ်။

Dart language overview — Dart

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Classes & Objects | Thuta Learning