ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Class က blueprint ဖြစ်ပြီး constructor ကတော့ အဲဒီ blueprint ကို ပြီးပြည့်စုံတဲ့ object တစ်ခုအဖြစ် ပြောင်းပေးတဲ့ ထုတ်လုပ်ရေးလိုင်းပါ။ Object တစ်ခုလိုအပ်တာအားလုံး ထွက်မသွားခင် လိုင်းပေါ်မှာ ရှိရပါမယ် — အင်ဂျင် မပါသေးတဲ့ ကားကို မပို့နိုင်ပါဘူး။ Dart က ဒါကို အလေးအနက်ထားလို့ constructor ရဲ့ အဓိကအလုပ်က field တိုင်းကို တန်ဖိုး ပေးဖို့ပါ။
Class တစ်ခုမှာ ထုတ်လုပ်ရေးလိုင်း အများကြီးလည်း ရှိနိုင်ပါတယ်။ ပုံမှန်လိုင်းက x နဲ့ y ကနေ Point ကို ဆောက်ပါတယ်၊ နောက်တစ်ခုက ဘာမှ မမေးဘဲ origin ကို ဆောက်ပါတယ်၊ တတိယတစ်ခုက list ထဲက ဂဏန်းနှစ်ခုကို ဖတ်ပါတယ်။ လိုင်းတစ်ခုစီမှာ ကိုယ်ပိုင်နာမည် ရှိလို့ အဲဒါတွေကို သုံးတဲ့ code က Point.origin() လို ဝါကျတစ်ခုလို ဖတ်ရပါတယ်။
Problem → Why → Solution
ပြဿနာ: final field ကို constructor body ထဲမှာ assign လုပ်လို့ မရဘူး
ပြောင်းလို့ မရတဲ့ Box တစ်ခု လိုချင်တယ် ဆိုပါစို့ — ဖန်တီးပြီးရင် size ကို ဘယ်တော့မှ မပြောင်းနိုင်ရအောင် field ကို final လုပ်ပါတယ်။ ပြီးတော့ တခြား language အများအပြားမှာ ရေးသလို parameter တစ်ခုယူပြီး body ထဲမှာ field ကို assign လုပ်တဲ့ constructor ရေးပါတယ်။ Dart က error နှစ်ခုနဲ့ ပယ်ပါတယ်: "The setter 'size' isn't defined for the type 'Box'." နဲ့ "Final field 'size' is not initialized." ဒုတိယ message က တကယ့်အဓိပ္ပာယ်ပါ — body မစခင် final field တိုင်းမှာ တန်ဖိုး ရှိပြီးသား ဖြစ်ရမယ်လို့ Dart က တောင်းဆိုတာပါ။
final ကို ဖယ်လိုက်ရင် error ပျောက်ပေမယ့် size ကို နောက်ပိုင်း code ဘယ်ဟာမဆို ပြောင်းလို့ရသွားပြီး ကာကွယ်ချင်ခဲ့တဲ့ bug အတိအကျ ဖြစ်လာပါတယ်။ Dart မှာ body မစခင် initialization ကို run ဖို့ နည်းလမ်းတစ်ခု လိုပြီး constructor ရဲ့ initializing formal နဲ့ initializer list တွေက အဲဒါအတွက်ပါ။
class Box {
final int size;
Box(int size) {
this.size = size; // too late: final fields must be set before the body runs
}
}Object ဖန်တီးတာမှာ အဆင့်နှစ်ဆင့် ရှိတယ်
Dart မှာ object ဖန်တီးတာက အဆင့်နှစ်ဆင့်ပါ: အရင် field တွေကို initialize လုပ်ပြီး ပြီးမှ constructor body က အပြည့်အဝ ဆောက်ပြီးသား object ပေါ်မှာ run ပါတယ်။ final field ကို ပထမအဆင့်မှာပဲ တန်ဖိုးပေးလို့ရပါတယ် — initializing formal (Box(this.size))၊ initializer list (Box(int s) : size = s) ဒါမှမဟုတ် field ကြေညာတဲ့နေရာမှာပါ။ Body run တဲ့အချိန်မှာတော့ field က ပိတ်ပြီးသားမို့ အဲဒီမှာ assign လုပ်တာ နောက်ကျသွားပါပြီ။
Core concept: generative, named နဲ့ initializer list
Generative constructor မှာ class နဲ့ နာမည်တူပြီး အလုပ်က field အားလုံး initialize ဖြစ်ပြီးသား object တစ်ခု ထုတ်ပေးဖို့ပါ။ Point(this.x, this.y); ဆိုတဲ့ အတိုကောက်ကို initializing formal လို့ ခေါ်ပါတယ်: this.x parameter တစ်ခုစီအတွက် Dart က body မ run ခင် argument ကို field x ထဲ copy ကူးပေးလို့ constructor အများစုမှာ body မလိုတော့ပါဘူး။ Parameter တွေက positional ဖြစ်နိုင်သလို ကွင်းထောင့်ထဲက optional positional ([this.age = 0]) ဒါမှမဟုတ် တွန့်ကွင်းထဲက named ({required this.name, this.age = 18}) လည်း ဖြစ်နိုင်ပါတယ်၊ argument နှစ်ခုသုံးခုထက် ပိုလာတဲ့အခါ named parameter က ပိုကောင်းပါတယ် — ခေါ်တဲ့နေရာကိုယ်တိုင်က documentation ဖြစ်လို့ပါ။
Dart မှာ နာမည်တူ constructor နှစ်ခု ခွင့်မပြုလို့ object ဖန်တီးတဲ့ တခြားနည်းတွေက ကိုယ်ပိုင်နာမည် ရပါတယ်: Point.origin(), Point.fromList(...)။ Named constructor တစ်ခုက redirecting initializer နဲ့ တခြား constructor ဆီ ပို့လို့လည်း ရပါတယ်၊ Point.unit() : this(1, 1);၊ ဒါကြောင့် တကယ့်အလုပ်က နေရာတစ်ခုတည်းမှာ ရှိနေပါတယ်။
Colon နောက်က အပိုင်းကို initializer list လို့ ခေါ်ပါတယ်။ Body မစခင် run ပြီး final field ကို တန်ဖိုးပေးလို့ရတဲ့ နေရာပါ (field ကိုယ်တိုင် ကြေညာတဲ့နေရာနဲ့ initializing formal နဲ့အပြင်): x = pair[0].toDouble()။ Constructor ရဲ့ parameter တွေကို ဖတ်လို့ရပေမယ့် this ကို မရပါဘူး၊ ဒါ့အပြင် superclass ကို ခေါ်တဲ့နေရာ (super(name)) နဲ့ assert(...) နဲ့ precondition စစ်တဲ့ နေရာလည်း ဖြစ်ပါတယ်။ Assert တွေကို debug run တွေမှာပဲ စစ်ပါတယ် — ဥပမာ dart run --enable-asserts နဲ့၊ Flutter debug build မှာ — user input ကို စစ်တာအတွက်တော့ အစားမထိုးနိုင်ပါဘူး။
What happens when you call Child()
----------------------------------
Child() is called
1. Child's field initializers
2. Child's initializer list
3. Parent constructor (its initializers first, then its body)
4. Child's body (runs last, on a fully initialized object)Class တွေ inherit လုပ်တဲ့အခါ အစဉ်က အရေးကြီးပါတယ်: ဖန်တီးနေတဲ့ class ရဲ့ field initializer တွေနဲ့ initializer list က အရင် run၊ ပြီးမှ superclass constructor က (သူ့ initializer တွေနဲ့ body အပါအဝင်) အပြည့်အဝ run၊ နောက်ဆုံးမှသာ ကိုယ်ခေါ်လိုက်တဲ့ class ရဲ့ body က run ပါတယ်။ အောက်က snippet က အဲဒီအစဉ်ကို print ထုတ်ပြပါတယ်။
String log(String message) {
print(message);
return message;
}
class Parent {
final String a = log('Parent field initializer');
Parent() {
print('Parent body');
}
}
class Child extends Parent {
final String b = log('Child field initializer');
final String c;
Child() : c = log('Child initializer list') {
print('Child body');
}
}
void main() {
Child();
}- Argument တွေ constructor ဆီ ရောက်
- Initializer list က field တွေ သတ်မှတ်
- Superclass constructor run
- Constructor body က နောက်ဆုံး run
Body run တဲ့အချိန်မှာ final field တိုင်းမှာ တန်ဖိုး ရှိပြီးသား ဖြစ်ရပါမယ်။
အထွေထွေ ပုံစံ
class Name {
final int a;
final int b;
Name(this.a, this.b); // generative, initializing formals
Name.alt(int value) : a = value, b = 0; // named + initializer list
Name.copy(Name o) : this(o.a, o.b); // redirecting
const Name.fixed() : a = 1, b = 2; // const (all fields final)
factory Name.cached() => _shared; // factory: may return an existing one
static final _shared = Name(0, 0);
}Colon နောက်က အပိုင်းက initializer list ပါ။ this(...) နဲ့ စရင် redirecting ဖြစ်ပြီး body မရှိရပါဘူး။ const constructor ရဲ့ field အားလုံးက final ဖြစ်ရပြီး factory ကတော့ this မရှိဘဲ တန်ဖိုးတစ်ခု return ပြန်ပါတယ်။
Example: Point ကို ဖန်တီးတဲ့ နည်းလမ်း ငါးမျိုး
ဒီ Point class မှာ constructor ငါးမျိုး ပါပါတယ်: ပုံမှန် constructor၊ named constructor နှစ်ခု (origin နဲ့ vertical)၊ initializer list ထဲမှာ assert နဲ့ type ပြောင်းလဲမှုပါတဲ့ fromList၊ တခြား constructor ဆီ ပို့တဲ့ redirecting unit() တို့ပါ။ Field နှစ်ခုလုံး final double ဖြစ်တာကို သတိထားပါ — Point(2, 3) မှာ ဂဏန်းတွေက int literal ပေမယ့် double context ထဲမှာ 2.0 နဲ့ 3.0 ဖြစ်သွားပါတယ်။ Run မလုပ်ခင် Point.unit() ဘာ print ထုတ်မလဲ ခန့်မှန်းကြည့်ပါ။
class Point {
final double x;
final double y;
Point(this.x, this.y);
Point.origin() : x = 0, y = 0;
Point.vertical(double yValue) : x = 0, y = yValue;
Point.fromList(List<num> pair)
: assert(pair.length == 2, 'need exactly two numbers'),
x = pair[0].toDouble(),
y = pair[1].toDouble();
Point.unit() : this(1, 1);
@override
String toString() => 'Point($x, $y)';
}
void main() {
print(Point(2, 3));
print(Point.origin());
print(Point.vertical(10));
print(Point.fromList([4, 5.5]));
print(Point.unit());
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Initializing formals
Point(this.x, this.y);this.x နဲ့ this.y က argument ကို final field ထဲ တိုက်ရိုက် ထည့်ပေးပါတယ်၊ body မလိုပါဘူး။ Point(2, 3) မှာ 2 နဲ့ 3 ကို field type double အရ 2.0 နဲ့ 3.0 အဖြစ် ပြောင်းပေးပါတယ်။
- Named + initializer list
Point.origin() : x = 0, y = 0;Point.origin() ကို ဘာ argument မှ မလိုဘဲ ခေါ်ပါတယ်။ Colon နောက်က initializer list က x နဲ့ y ကို သတ်မှတ်ပါတယ်၊ ဒါကြောင့် နာမည်ကပဲ object ရဲ့ ရည်ရွယ်ချက်ကို ပြောပြပါတယ်။
- Parameter feeds the list
Point.vertical(double yValue) : x = 0, y = yValue;Initializer list က constructor parameter (yValue) ကို ဖတ်လို့ရပါတယ်။ x ကို 0 ပေးပြီး y ကို parameter ကနေ ယူလို့ Point.vertical(10) က (0.0, 10.0) ဖြစ်ပါတယ်။
- assert + conversion
x = pair[0].toDouble(),fromList က List<num> ကို လက်ခံပြီး toDouble() နဲ့ field type ကို ကိုက်အောင် ပြောင်းပါတယ်။ အပေါ်က assert က ဂဏန်းနှစ်ခုမဟုတ်ရင် (debug run မှာ) ပြဿနာကို စောစီးစွာ ပြပေးပါတယ်၊ Point.fromList([4, 5.5]) က (4.0, 5.5) ပါ။
- Redirecting
Point.unit() : this(1, 1);this(1, 1) က လုပ်ငန်းအားလုံးကို ပင်မ constructor ဆီ လွှဲပေးပါတယ်။ Redirecting constructor မှာ body မပါရပါဘူး၊ ဒါကြောင့် Point.unit() က Point(1, 1) နဲ့ အတူတူ (1.0, 1.0) ပါ။
Point(2.0, 3.0)
Point(0.0, 0.0)
Point(0.0, 10.0)
Point(4.0, 5.5)
Point(1.0, 1.0)Named parameter, required နဲ့ super parameter
{required this.name, this.age = 18, this.email} လို named parameter တွေကို နာမည်နဲ့ ခေါ်ပါတယ်: User(name: 'Aye', email: 'a@x.com')။ required ပါတာကို မပို့ရင် compile error ဖြစ်ပြီး default (this.age = 18) ပါတာကို ချန်လို့ရပါတယ်၊ default မပါတဲ့ nullable (this.email) က မပို့ရင် null ဖြစ်ပါတယ်။ [this.age = 0] လို optional positional ကိုလည်း သုံးနိုင်ပေမယ့် argument များလာရင် named က ဖတ်ရပိုလွယ်ပါတယ်။ Subclass မှာ Dog(super.name, this.breed) လို super parameter က argument ကို parent constructor ဆီ တိုက်ရိုက် ပို့ပေးလို့ Animal ရဲ့ name ကို ထပ်ကြေညာစရာ မလိုပါဘူး။ Named constructor ဆီက User.guest() : this(name: 'guest'); လို redirect ကလည်း named argument တွေ သုံးလို့ ရပါတယ်။
class User {
final String name;
final int age;
final String? email;
User({required this.name, this.age = 18, this.email});
User.guest() : this(name: 'guest');
User.positional(this.name, [this.age = 0]) : email = null;
}
class Animal {
final String name;
Animal(this.name);
}
class Dog extends Animal {
final String breed;
Dog(super.name, this.breed);
}
void main() {
final u = User(name: 'Aye', email: 'a@x.com');
print('${u.name} ${u.age} ${u.email}');
final g = User.guest();
print('${g.name} ${g.age} ${g.email}');
final p = User.positional('Su');
print('${p.name} ${p.age} ${p.email}');
final d = Dog('Rex', 'lab');
print('${d.name} ${d.breed}');
}const constructor နဲ့ canonical instance
const constructor က compile လုပ်ချိန်မှာ တည်ဆောက်လို့ရတဲ့ immutable object တွေကို ဖန်တီးပါတယ်။ လိုအပ်ချက်က အဲဒီ class ရဲ့ field အားလုံး final ဖြစ်ရခြင်းနဲ့ constructor ခေါ်တဲ့နေရာမှာ const (const Color(1, 2, 3)) ရေးရခြင်းပါ။ တန်ဖိုးတူတဲ့ const expression နှစ်ခုက memory ထဲမှာ object တစ်ခုတည်း (canonical instance) ကို မျှဝေသုံးပါတယ်၊ const မပါဘဲ ခေါ်ရင်တော့ အကြိမ်တိုင်း object အသစ် ဖြစ်ပါတယ်။ Argument တွေက compile-time constant မဟုတ်ရင် const မရပါဘူး: var n = 5; const Color(n, 0, 0) က "Not a constant expression." ပါ။ ရလဒ်မှာ const object က cache လုပ်ထားတာနဲ့ တူပြီး Flutter က const widget တွေကို rebuild မှာ instance တစ်ခုတည်းအဖြစ် ပြန်သုံးနိုင်ပါတယ်။
class Color {
final int r, g, b;
const Color(this.r, this.g, this.b);
}
void main() {
const c1 = Color(1, 2, 3);
const c2 = Color(1, 2, 3);
final c3 = Color(1, 2, 3);
final c4 = Color(1, 2, 3);
print(identical(c1, c2)); // true: one shared constant
print(identical(c3, c4)); // false: two separate objects
print(identical(c1, c3)); // false
}factory constructor: cache နဲ့ subtype ပြန်ခြင်း
Factory constructor က generative တစ်ခုလို object အသစ်ကို အမြဲ ဖန်တီးဖို့ မလိုပါဘူး — return statement နဲ့ ရှိပြီးသား instance (cache) ဒါမှမဟုတ် subtype တစ်ခုကို ပြန်ပေးလို့ ရပါတယ်။ ဒါကြောင့် Logger('db') ကို နှစ်ခါခေါ်ရင် object တစ်ခုတည်းကို ရပြီး Shape.fromJson က json ပေါ်မူတည်ပြီး Square ဒါမှမဟုတ် Rect ကို ပြန်ပေးပါတယ်။ အကန့်အသတ်ကတော့ factory မှာ this မရှိပြီး initializer list လည်း မသုံးနိုင်တာပါ၊ field တွေကို သူ့ရဲ့ ပြန်ပေးတဲ့ object ကို ဖန်တီးတဲ့ constructor ကပဲ initialize လုပ်ပါတယ်။ _internal လို private named constructor နဲ့ တွဲပြီး ပြင်ပက တိုက်ရိုက်မခေါ်နိုင်အောင် လုပ်တာ အသုံးများပါတယ်။
class Logger {
static final Map<String, Logger> _cache = {};
final String name;
factory Logger(String name) {
return _cache.putIfAbsent(name, () => Logger._internal(name));
}
Logger._internal(this.name) {
print('creating logger $name');
}
void log(String message) => print('[$name] $message');
}
abstract class Shape {
double get area;
factory Shape.fromJson(Map<String, Object> json) {
return switch (json['type']) {
'square' => Square(json['side'] as double),
'rect' => Rect(json['w'] as double, json['h'] as double),
_ => throw FormatException('unknown shape: ${json['type']}'),
};
}
}
class Square implements Shape {
final double side;
Square(this.side);
@override
double get area => side * side;
}
class Rect implements Shape {
final double w, h;
Rect(this.w, this.h);
@override
double get area => w * h;
}
void main() {
final a = Logger('db');
final b = Logger('db');
final c = Logger('web');
print(identical(a, b));
print(identical(a, c));
a.log('hello');
print(Shape.fromJson({'type': 'square', 'side': 3.0}).area);
print(Shape.fromJson({'type': 'rect', 'w': 2.0, 'h': 5.5}).area);
try {
Shape.fromJson({'type': 'blob'});
} on FormatException catch (e) {
print(e.message);
}
}| Constructor အမျိုးအစား | ရေးပုံ | ဘယ်အချိန် သုံးမလဲ |
|---|---|---|
| Generative | Point(this.x, this.y); | ပုံမှန် ဖန်တီးပြီး initialize လုပ်တဲ့ နည်း |
| Named | Point.origin() | ဖန်တီးတဲ့ တခြားနည်းတွေကို အဓိပ္ပာယ်ရှိတဲ့ နာမည်ပေးချင်ရင် |
| Redirecting | Point.unit() : this(1, 1); | တခြား constructor ဆီ အလုပ်လွှဲချင်ရင် |
| const | const Color(1, 2, 3) | ပြောင်းမရတဲ့ compile-time constant object တွေ |
| factory | factory Logger(name) | Cache ထဲက ဒါမှမဟုတ် subtype instance ပြန်ချင်ရင် |
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
JSON model တွေက အသုံးအများဆုံး ဥပမာပါ: User.fromJson(Map<String, Object?> json) က decode လုပ်ထားတဲ့ map ကို object အဖြစ် ပြောင်းပေးတဲ့ named constructor (ဒါမှမဟုတ် factory) ပါ။ Flutter widget တွေကို const Greeting({super.key, required this.name}) လို constructor တွေနဲ့ အားလုံးနီးပါး ရေးပါတယ် — const က Flutter ကို instance တစ်ခုတည်း ပြန်သုံးခွင့်ပေးပြီး required က name မေ့မထည့်မိအောင် တားပါတယ်။ Core library ကိုယ်တိုင်လည်း အယူအဆတူတူ သုံးပါတယ်: DateTime.now(), Duration(minutes: 2), List.filled(3, 0) နဲ့ List.generate တို့က named ဒါမှမဟုတ် factory constructor တွေပါ။
Factory constructor တွေက shared Logger ဒါမှမဟုတ် database connection လို cache နဲ့ singleton တွေကို အားပေးပြီး const constructor တွေက ပြောင်းမရတဲ့ configuration ကို တစ်ကြိမ် ကြေညာပြီး နေရာတိုင်း ပြန်သုံးခွင့်ပေးပါတယ်။ ကိုယ်ပိုင် class ဒီဇိုင်းလုပ်တဲ့အခါ object က တစ်ဝက်တစ်ပျက် ဆောက်ထားတဲ့ အခြေအနေမှာ မှန်ကန်နေနိုင်လား မေးပါ — မမှန်ရင် constructor ကိုပဲ တစ်ခုတည်းသော ဝင်ပေါက် လုပ်ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. final field ကို constructor body ထဲမှာ assign လုပ်တာ
Box(int size) { this.size = size; } က "Final field 'size' is not initialized." ဖြစ်ပါတယ်၊ body မစခင် field တန်ဖိုး ရှိရလို့ပါ။ Box(this.size); လို initializing formal သုံးပါ၊ ဒါမှမဟုတ် Box(int s) : size = s; လို initializer list သုံးပါ။
2. Initializer list ထဲမှာ this ကို သုံးတာ
Rect(this.w, this.h) : area = this.w * this.h; က "Can't access 'this' in a field initializer." ဖြစ်ပါတယ်၊ ဒီအချိန်မှာ object ကို မဆောက်ပြီးသေးလို့ပါ။ Parameter နာမည်တွေကိုပဲ သုံးပါ: area = w * h; — ဒီ w, h တွေက constructor parameter တွေဖြစ်ပြီး field မဟုတ်ပါဘူး။
3. const ရေးဖို့ မေ့တာ၊ constant မဟုတ်တဲ့ argument ထည့်တာ
const constructor ရှိရုံနဲ့ instance မျှဝေတာ မဖြစ်ပါဘူး — ခေါ်တဲ့နေရာမှာလည်း const ရေးမှသာ identical(Color(1, 2, 3), Color(1, 2, 3)) နဲ့ မတူဘဲ const တူတူနှစ်ခုက true ပါ။ var n = 5; const Score(n) လို argument က constant မဟုတ်ရင် "Not a constant expression." ဖြစ်ပါတယ်၊ const ကို ဖယ်ပါ ဒါမှမဟုတ် argument ကို const ပေးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Initializing formal ကို ဦးစားပေးပြီး constructor body ကို ဗလာထားနိုင်သမျှ ထားပါ။
- ဖန်တီးတဲ့ တခြားနည်းတွေအတွက် flag parameter မထည့်ဘဲ named constructor သုံးပါ — Point.origin() က Point(0, 0, isOrigin: true) ထက် ပိုရှင်းပါတယ်။
- Class တစ်ခုရဲ့ field အားလုံး final ဖြစ်ရင် constructor ကို const ပေးပါ၊ ဒါဆို တန်ဖိုးတူ object တွေကို မျှဝေသုံးနိုင်ပါတယ်။
- Programmer အမှားအတွက် initializer list ထဲမှာ assert ကို သုံးပြီး user ဆီက ဝင်လာတဲ့ မမှန်တဲ့ input အတွက်တော့ exception ပစ်ပါ — assert က release မှာ မစစ်ပါဘူး။
Mini challenge
Mini challenge: Money class
ပမာဏကို cent နဲ့ သိမ်းတဲ့ Money class ကို constructor လေးမျိုးနဲ့ ရေးပါ — ပင်မ const constructor၊ redirecting named constructor၊ const named constructor နဲ့ စာသားကို parse လုပ်တဲ့ factory ပါ။
- final int cents နဲ့ final String currency ကို initializing formal သုံးတဲ့ const ပင်မ constructor ဖြင့် သတ်မှတ်ပါ
- Money.dollars(int dollars) က ပင်မ constructor ဆီ redirect လုပ်ပြီး 'USD' နဲ့ dollars * 100 cents ပေးပါ
- const Money.zero() က initializer list နဲ့ cents ကို 0၊ currency ကို 'USD' သတ်မှတ်ပါ
- factory Money.parse(String text) က '7.25 EUR' ကို Money(725, 'EUR') အဖြစ် ပြောင်းပြီး ပုံစံမမှန်ရင် FormatException ပစ်ပါ
- toString က '12.50 USD' ပုံစံ ထုတ်ပါ၊ main() မှာ constructor တစ်ခုစီနဲ့ identical(const Money.zero(), const Money.zero()) နဲ့ parse မအောင်မြင်တဲ့ message ကို print ထုတ်ပါ
class Money {
// TODO: final cents (int) and currency (String)
// TODO: const main constructor with initializing formals
// TODO: Money.dollars(int dollars): redirect to the main constructor, USD
// TODO: const Money.zero(): set both fields in an initializer list
// TODO: factory Money.parse(String text): "7.25 EUR" -> Money(725, 'EUR'),
// throw a FormatException when the text does not match
// TODO: toString printing "12.50 USD"
}
void main() {
// TODO: print const Money(1250, 'USD'), Money.dollars(5), Money.zero(),
// identical(const Money.zero(), const Money.zero()),
// Money.parse('7.25 EUR'), and the message from a failed parse
}ရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: 12.50 USD 5.00 USD 0.00 USD true 7.25 EUR bad input: expected "12.50 USD" but got "lots"
အရိပ်အမြွက် ကြည့်ရန်
Redirecting constructor က Money.dollars(int dollars) : this(dollars * 100, 'USD'); ပုံစံပါ — body မပါပါဘူး။ Factory ထဲမှာ text.split(' ') နဲ့ ခွဲပြီး double.tryParse(parts[0]) ကို စစ်ပါ၊ null ဆိုရင် throw ပါ၊ မဟုတ်ရင် Money((amount * 100).round(), parts[1]) ကို return ပါ။ toString မှာ cents ~/ 100 နဲ့ cents % 100 ကို သုံးပြီး padLeft(2, '0') နဲ့ cent ကို နှစ်လုံးပြည့်အောင် ဖြည့်ပါ။
အဖြေ ကြည့်ရန်
class Money {
final int cents;
final String currency;
const Money(this.cents, this.currency);
Money.dollars(int dollars) : this(dollars * 100, 'USD');
const Money.zero() : cents = 0, currency = 'USD';
factory Money.parse(String text) {
final parts = text.split(' ');
final amount = parts.length == 2 ? double.tryParse(parts[0]) : null;
if (amount == null) {
throw FormatException('expected "12.50 USD" but got "$text"');
}
return Money((amount * 100).round(), parts[1]);
}
@override
String toString() {
final units = (cents ~/ 100).toString();
final rest = (cents % 100).toString().padLeft(2, '0');
return '$units.$rest $currency';
}
}
void main() {
print(const Money(1250, 'USD'));
print(Money.dollars(5));
print(Money.zero());
print(identical(const Money.zero(), const Money.zero()));
print(Money.parse('7.25 EUR'));
try {
Money.parse('lots');
} on FormatException catch (e) {
print('bad input: ${e.message}');
}
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Initializing formal (this.x) က argument ကို body မစခင် field ထဲ ထည့်ပေးလို့ constructor အများစုမှာ body မလိုပါဘူး။ final field ကို ဒီနည်းနဲ့ ဒါမှမဟုတ် initializer list နဲ့ပဲ သတ်မှတ်ရပါတယ်။
- Named constructor တွေက ဖန်တီးတဲ့ တခြားနည်းတွေကို နာမည်ပေးပြီး redirecting constructor က this(...) နဲ့ အလုပ်ကို ပင်မ constructor ဆီ လွှဲပါတယ်။
- Run အစဉ်က Child ရဲ့ field initializer နဲ့ initializer list၊ ပြီးရင် Parent constructor အပြည့်အဝ၊ နောက်ဆုံး Child ရဲ့ body ပါ။
- const constructor က ပြောင်းမရတဲ့ canonical object တွေ ဖန်တီးပြီး factory က cache ထဲက ဒါမှမဟုတ် subtype instance ပြန်နိုင်ပေမယ့် this မရှိပါဘူး။
Dart language overview — Dart