ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
တိရစ္ဆာန်တိုင်းမှာ နာမည် ရှိပြီး အသံထွက်နိုင်ပါတယ်။ ခွေးကလည်း တိရစ္ဆာန်တစ်ကောင် ဖြစ်လို့ အဲဒီ အရည်အချင်းတွေ အကုန်ရှိပြီး ဘောလုံးပြန်ယူတာလို ခွေးမှာပဲ ရှိတဲ့ အလုပ်လည်း ထပ်လုပ်နိုင်ပါတယ်။ Inheritance ဆိုတာ ဒီလို "Dog က Animal တစ်မျိုး ဖြစ်တယ်" ဆိုတဲ့ ဆက်ဆံရေးကို code ထဲမှာ ရေးတာပါ။
JavaScript မှာ class Dog extends Animal လို့ ရေးလိုက်ရင် Dog က Animal ရဲ့ method အားလုံးကို ကူးရေးစရာ မလိုဘဲ ရပါတယ်။ Dog မှာ ကွဲပြားချင်တဲ့ method (ဥပမာ speak) ကိုတော့ နာမည်တူနဲ့ ပြန်ရေးပြီး ကိုယ်ပိုင်ပုံစံ ပေးလို့ရပါတယ်။
Problem → Why → Solution
ပြဿနာ: ကူးရေးထားတဲ့ code မှာ ပြင်ဆင်မှု တစ်ဝက်ပဲ ရောက်တယ်
App တစ်ခုမှာ email နဲ့ SMS notification class နှစ်ခု ရှိပြီး recipient စစ်တဲ့ code ကို နှစ်နေရာလုံးမှာ ကူးရေးထားပါတယ်။ Recipient အလွတ် ဖြစ်နေတာကို စစ်တဲ့ bug fix ကို EmailNotification မှာ ထည့်ပေမယ့် SmsNotification မှာ ထည့်ဖို့ မေ့သွားပါတယ်။ ရလဒ်က SMS တွေ ဘယ်သူ့ဆီမှ မရောက်ဘဲ error မတက်ဘဲ ပျောက်သွားတာပါ။
class EmailNotification {
constructor(to) {
if (!to) throw new Error("Recipient required"); // fixed here...
this.to = to;
}
describe() { return `Notification for ${this.to}`; }
}
class SmsNotification {
constructor(to) {
this.to = to; // ...but not here
}
describe() { return `Notification for ${this.to}`; } // copied again
}
new SmsNotification(""); // no error: an SMS to nobodyဘာကြောင့် ဒီလို ဖြစ်တာလဲ
Class နှစ်ခုလုံးမှာ "notification ဖြစ်ခြင်း" ဆိုတဲ့ တူညီတဲ့ အပိုင်း ရှိပေမယ့် အဲဒီအပိုင်းကို တစ်နေရာတည်းမှာ မထားဘဲ နှစ်နေရာ ကူးထားတာပါ။ Shared logic ကို Notification ဆိုတဲ့ parent class ထဲ ရွှေ့ပြီး class နှစ်ခုလုံးက extends လုပ်လိုက်ရင် fix တစ်ခုက child အားလုံးကို တစ်ပြိုင်နက် ရောက်ပါတယ်။
Core concept: extends, super နဲ့ override
class Dog extends Animal က ချိတ်ဆက်မှု နှစ်ခု လုပ်ပါတယ်။ Dog.prototype ကို Animal.prototype နဲ့ ချိတ်လို့ dog object တွေက Animal ရဲ့ instance method တွေကို ရှာတွေ့ပြီး Dog ကိုယ်တိုင်ကိုလည်း Animal နဲ့ ချိတ်လို့ static method တွေကိုပါ အမွေရပါတယ်။ dog.speak() ကို ခေါ်ရင် JavaScript က dog ပေါ်မှာ အရင်ရှာ၊ ပြီးမှ Dog.prototype၊ ပြီးမှ Animal.prototype ဆီ အဆင့်ဆင့် တက်ရှာပြီး ပထမဆုံး တွေ့တဲ့ဟာကို သုံးပါတယ်။
Method lookup for dog.speak() and dog.toString()
------------------------------------------------
dog own data: name, breed
|
v [[Prototype]]
Dog.prototype speak(), fetch() <- speak found here first
|
v
Animal.prototype speak() <- hidden by Dog's speak
|
v
Object.prototype toString(), hasOwnProperty()Child class မှာ constructor ရေးရင် this ကို မသုံးခင် super(...) ကို မဖြစ်မနေ ခေါ်ရပါတယ်။ Derived class မှာ object ကို parent constructor က ဖန်တီး/initialize လုပ်ပေးတာမို့ super() မခေါ်ခင် this ဆိုတာ မရှိသေးပါဘူး — this.breed = breed ကို super() အရင် ရေးရင် ReferenceError တက်ပါတယ်။ Child မှာ constructor လုံးဝ မရေးရင်တော့ JavaScript က argument အားလုံးကို super() ဆီ လက်ဆင့်ကမ်းတဲ့ default constructor ကို ထည့်ပေးပါတယ်။
Child မှာ parent နဲ့ နာမည်တူ method ရေးတာကို override လို့ ခေါ်ပြီး child ရဲ့ version က parent ရဲ့ version ကို ဖုံးလိုက်ပါတယ်။ Parent ရဲ့ အလုပ်ကို လုံးဝ အစားမထိုးဘဲ ထပ်ဖြည့်ချင်ရင် method ထဲကနေ super.speak() လို့ ခေါ်လို့ရပါတယ်။ Inheritance ကို "Dog is an Animal" လို is-a ဆက်ဆံရေး တကယ်ရှိပြီး parent ရဲ့ contract တည်ငြိမ်တဲ့အခါမှ သုံးပါ။ "Car has an Engine" လို has-a ဆက်ဆံရေးဆိုရင် object တစ်ခုထဲမှာ တခြား object ကို property အဖြစ် ထည့်တဲ့ composition ကို သုံးပါ။
Object တစ်ခုက ဘယ် class မိသားစုထဲ ပါလဲ စစ်ချင်ရင် instanceof ကို သုံးပါတယ်။ dog instanceof Animal က dog ရဲ့ prototype chain တစ်လျှောက်မှာ Animal.prototype ကို တွေ့လား စစ်တာဖြစ်လို့ true ရပါတယ်။ Inheritance အဆင့် တစ်ဆင့်တိုးတိုင်း ဒီ chain က တစ်ဆင့် ရှည်လာပြီး parent ကို ပြင်လိုက်တာက child အားလုံးကို သက်ရောက်ပါတယ်။ ဒါကြောင့် အဆင့် နှစ်ဆင့်၊ သုံးဆင့်ထက် နက်တဲ့ hierarchy တွေက နားလည်ရ ခက်ပြီး ပြင်ရတာ ကြောက်စရာ ကောင်းလာပါတယ်။
- Child constructor က super() အရင်ခေါ်
- Parent က this ကို setup လုပ်
- Method ကို child မှာ အရင်ရှာ
- မတွေ့ရင် parent ဆီ တက်ရှာ
Child မှာ ရှိတဲ့ method က အမြဲ အနိုင်ရပါတယ်၊ parent ကို ခေါ်ချင်ရင် super.method() လို့ တိတိကျကျ ပြောရပါတယ်။
အထွေထွေ ပုံစံ
class Child extends Parent {
constructor(parentArg, childArg) {
super(parentArg); // must come before this
this.extra = childArg;
}
sharedMethod() { // override
const base = super.sharedMethod();
return base + " plus child behavior";
}
}extends က Parent နဲ့ ချိတ်ပြီး super(parentArg) က parent constructor ကို ခေါ်ပါတယ်။ နာမည်တူ method ရေးရင် override ဖြစ်ပြီး super.sharedMethod() နဲ့ parent ရဲ့ ရလဒ်ကို ယူသုံးနိုင်ပါတယ်။
Example: Animal, Dog နဲ့ Cat
Animal က name နဲ့ speak() ကို ပေးပြီး Dog နဲ့ Cat က speak() ကို ကိုယ့်ပုံစံနဲ့ override လုပ်ပါတယ်။ Dog မှာ fetch() ဆိုတဲ့ method အသစ် ထပ်ပါပြီး နောက်ဆုံးစာကြောင်းက instanceof နဲ့ ဆက်ဆံရေးကို စစ်ပြထားပါတယ်။ မ run ခင် object တစ်ခုစီက ဘယ် speak() ကို သုံးမလဲ ကြိုခန့်မှန်းကြည့်ပါ။
class Animal {
constructor(name) {
this.name = name;
}
speak() {
return `${this.name} makes a sound`;
}
}
class Dog extends Animal {
constructor(name, breed) {
super(name); // call the parent constructor first
this.breed = breed;
}
speak() {
return `${this.name} barks: Woof!`;
}
fetch() {
return `${this.name} is fetching the ball`;
}
}
class Cat extends Animal {
constructor(name, color) {
super(name);
this.color = color;
}
speak() {
return `${this.name} meows: Meow!`;
}
}
const dog = new Dog("Buddy", "Golden Retriever");
const cat = new Cat("Whiskers", "Orange");
console.log(dog.speak());
console.log(dog.fetch());
console.log(cat.speak());
console.log(dog instanceof Animal, cat instanceof Dog);Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Line 11
class Dog extends Animal {Dog.prototype ကို Animal.prototype နဲ့ ချိတ်လိုက်လို့ Dog မှာ မရေးထားတဲ့ method တွေကို Animal ဆီက ယူသုံးပါတယ်။
- Line 13
super(name); // call the parent constructor firstAnimal ရဲ့ constructor ကို run ပြီး this.name ကို သတ်မှတ်ပေးပါတယ်။ ဒီစာကြောင်း မတိုင်ခင် this ကို သုံးရင် ReferenceError တက်ပါတယ်။
- Line 14
this.breed = breed;super() ပြီးမှ this ကို သုံးလို့ရပြီး Dog အတွက်ပဲ ရှိတဲ့ breed ကို ထပ်ထည့်ပါတယ်။
- Lines 17-19
speak() {Animal ရဲ့ speak() ကို override လုပ်ပါတယ်။ dog.speak() ကို ခေါ်တော့ lookup က Dog.prototype မှာ အရင်တွေ့လို့ "makes a sound" မဟုတ်ဘဲ "barks: Woof!" ရပါတယ်။
- Line 43
console.log(dog instanceof Animal, cat instanceof Dog);instanceof က prototype chain ကို တက်စစ်ပါတယ်။ dog ရဲ့ chain မှာ Animal.prototype ပါလို့ true ဖြစ်ပြီး Cat နဲ့ Dog က ညီအစ်ကိုသာ ဖြစ်လို့ cat instanceof Dog က false ပါ။
Buddy barks: Woof!
Buddy is fetching the ball
Whiskers meows: Meow!
true falseOverride လုပ်ပြီး parent ကိုလည်း ဆက်သုံးခြင်း
Override တိုင်းက parent ကို အစားထိုးဖို့ မလိုပါဘူး။ အောက်က UrgentNotification က message ကို စာလုံးကြီးပြောင်းပြီး ကျန်တဲ့ format လုပ်တာကို super.send() ဆီ လွှဲပေးပါတယ်။ Parent ရဲ့ format ပြောင်းသွားရင် child က အလိုအလျောက် လိုက်ပြောင်းပါတယ်။ UrgentNotification မှာ constructor မရေးထားတာကိုလည်း သတိထားပါ — default constructor က "ops@example.com" ကို parent ဆီ လက်ဆင့်ကမ်းပေးလို့ this.to ကို ရပါတယ်။ super.send() ထဲမှာ this က UrgentNotification instance ပဲ ဖြစ်နေဆဲပါ။
class Notification {
constructor(to) { this.to = to; }
send(text) { return `To ${this.to}: ${text}`; }
}
class UrgentNotification extends Notification {
send(text) {
return super.send(text.toUpperCase() + "!");
}
}
console.log(new UrgentNotification("ops@example.com").send("disk full"));
// To ops@example.com: DISK FULL!လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Notification system တစ်ခုမှာ Notification base class က recipient စစ်တာ၊ retry လုပ်တာ၊ log ရေးတာကို တစ်နေရာတည်းမှာ လုပ်ပြီး EmailNotification နဲ့ SmsNotification က deliver() ကိုပဲ override လုပ်ပါတယ်။ Push notification အသစ် ထည့်ချင်ရင် class အသစ်တစ်ခု extends လုပ်ပြီး deliver() ရေးရုံပါပဲ။
JavaScript ecosystem ထဲမှာလည်း extends ကို မကြာခဏ တွေ့ရပါတယ်။ Custom error တွေကို class ValidationError extends Error လို့ ရေးပြီး (နောက် lesson တွေမှာ ပါပါမယ်) browser ရဲ့ Web Component တွေကို class MyButton extends HTMLElement လို့ ရေးပါတယ်။ React project အဟောင်းတွေမှာလည်း class component တွေက React.Component ကို extends လုပ်ထားတာ တွေ့ရပါမယ်။ ဒီနေရာတွေ အားလုံးမှာ child က parent ရဲ့ contract ကို လိုက်နာပြီး လိုတဲ့ method ကိုပဲ override လုပ်ပါတယ်။
Code review မှာ inheritance တစ်ခုကို စစ်တဲ့အခါ "parent ကို သုံးတဲ့ နေရာတိုင်းမှာ child ကို အစားထိုးထည့်ရင် အလုပ်လုပ်သေးလား" လို့ မေးကြည့်ပါ။ sendAll(notifications) လို function က Notification ဘယ်အမျိုးအစားကို ပေးပေး ပြဿနာမရှိရင် hierarchy က ကျန်းမာပါတယ်။ Child တစ်ခုကြောင့် if (n instanceof SmsNotification) လို special case တွေ ထည့်ရရင်တော့ design ကို ပြန်စဉ်းစားဖို့ အချိန်ပါ။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. super() မတိုင်ခင် this ကို သုံးတာ
constructor(name, breed) { this.breed = breed; super(name); } လို့ ရေးရင် "ReferenceError: Must call super constructor in derived class before accessing 'this'" တက်ပါတယ်။ super() ကို constructor ရဲ့ ပထမဆုံး statement အဖြစ် ထားတာ အလွယ်ဆုံးပါ။ super() ကို လုံးဝ မခေါ်ဘဲ constructor ပြီးသွားရင်လည်း ReferenceError ပဲ ရပါတယ်။
2. Override လုပ်ရင်း parent ရဲ့ အလုပ်ကို မတော်တဆ ဖျက်တာ
Parent ရဲ့ send() က recipient စစ်ပြီး log ရေးတယ်ဆိုပါစို့။ Child မှာ send() ကို အသစ်ပြန်ရေးပြီး super.send() မခေါ်မိရင် အဲဒီ စစ်ဆေးမှုနဲ့ log အားလုံး တိတ်တဆိတ် ပျောက်သွားပါတယ်။ Override တိုင်းမှာ parent ရဲ့ အလုပ်ကို အစားထိုးချင်တာလား ဖြည့်ချင်တာလား သေချာ ဆုံးဖြတ်ပါ။
3. is-a မဟုတ်ဘဲ code ပြန်သုံးဖို့သက်သက် extends လုပ်တာ
class Stack extends Array လို့ ရေးလိုက်ရင် push/pop ရပေမယ့် splice(), shift(), stack[0] = ... လို Stack စည်းမျဉ်း (နောက်ဆုံးထည့်တာ အရင်ထွက်) ကို ချိုးဖောက်တဲ့ method တွေပါ ပါလာပါတယ်။ Stack ထဲမှာ #items array တစ်ခု ထားပြီး push/pop ကိုပဲ ထုတ်ပေးတဲ့ composition က ပိုလုံခြုံပါတယ်။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- Hierarchy ကို တိမ်တိမ်ထားပါ — base class တစ်ခုနဲ့ child တစ်ဆင့်က အများအားဖြင့် လုံလောက်ပါတယ်။
- Child constructor ထဲမှာ super() ကို ပထမဆုံး စာကြောင်းအဖြစ် ရေးပြီး child အတွက်ပဲ လိုတဲ့ field တွေကို အောက်မှာ ထည့်ပါ။
- Child တွေက override လုပ်ရမယ့် method တွေ (ဥပမာ deliver) ကို base class မှာ ရှင်းရှင်းလင်းလင်း ကြေညာပြီး comment နဲ့ မှတ်ထားပါ။
- Code ပြန်သုံးဖို့ပဲ လိုရင် extends အစား helper function ဒါမှမဟုတ် composition ကို အရင် စဉ်းစားပါ။
Mini challenge
Mini challenge: Email နဲ့ SMS notification
Problem section က ကူးရေးထားတဲ့ code ကို ဖယ်ပြီး Notification base class တစ်ခုနဲ့ child class နှစ်ခု ရေးပါ။ Message အလွတ် စစ်တာကို base class မှာ တစ်ခါပဲ ရေးရပါမယ်။
- Notification က recipient ကို သိမ်းပြီး send(message) မှာ message အလွတ်ဆိုရင် "Not sent to <recipient>: empty message" ကို ပြန်ပေးရမယ်၊ မဟုတ်ရင် this.deliver(message) ကို ခေါ်ရမယ်
- EmailNotification(recipient, subject) က super() ခေါ်ပြီး subject ကို သိမ်းကာ deliver() ကို override လုပ်ရမယ်
- SmsNotification က constructor မရေးဘဲ deliver() ကိုပဲ override လုပ်ပြီး စာလုံး 20 ထက်ရှည်ရင် 17 လုံးနဲ့ "..." ဖြတ်ရမယ်
- Notification နှစ်ခုကို array ထဲထည့်ပြီး loop တစ်ခုတည်းနဲ့ send() ခေါ်ရမယ်
class Notification {
constructor(recipient) {
// TODO: store the recipient
}
send(message) {
// TODO: refuse blank messages, otherwise return this.deliver(message)
}
deliver(message) {
// TODO: a generic fallback delivery string
}
}
class EmailNotification extends Notification {
// TODO: constructor(recipient, subject) that calls super first
// TODO: deliver(message) -> "Email to <recipient> [<subject>]: <message>"
}
class SmsNotification extends Notification {
// TODO: deliver(message) -> "SMS to <recipient>: <message, shortened>"
}
// TODO: send "Your order has shipped today" through both, then a blank SMSရလဒ်: Console မှာ အောက်ပါ သုံးကြောင်း ထွက်ရပါမယ်: Email to su@example.com [Order]: Your order has shipped today SMS to +95 9 123 456: Your order has sh... Not sent to +95 9 123 456: empty message
အရိပ်အမြွက် ကြည့်ရန်
message.trim() က string အလွတ်ဆိုရင် falsy ဖြစ်လို့ if (!message.trim()) နဲ့ စစ်နိုင်ပါတယ်။ send() ထဲက this.deliver() က object ရဲ့ class ပေါ်မူတည်ပြီး child ရဲ့ override ကို ရှာတွေ့ပါတယ်။
အဖြေ ကြည့်ရန်
class Notification {
constructor(recipient) {
this.recipient = recipient;
}
send(message) {
if (!message.trim()) return `Not sent to ${this.recipient}: empty message`;
return this.deliver(message);
}
deliver(message) {
return `Sending "${message}" to ${this.recipient}`;
}
}
class EmailNotification extends Notification {
constructor(recipient, subject) {
super(recipient);
this.subject = subject;
}
deliver(message) {
return `Email to ${this.recipient} [${this.subject}]: ${message}`;
}
}
class SmsNotification extends Notification {
deliver(message) {
const short = message.length > 20 ? message.slice(0, 17) + "..." : message;
return `SMS to ${this.recipient}: ${short}`;
}
}
const notifications = [
new EmailNotification("su@example.com", "Order"),
new SmsNotification("+95 9 123 456"),
];
for (const n of notifications) {
console.log(n.send("Your order has shipped today"));
}
console.log(notifications[1].send(" "));Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- class Child extends Parent က prototype chain ကို ချိတ်ပြီး child က parent ရဲ့ method တွေကို အမွေရပါတယ်။
- Child constructor ထဲမှာ this မသုံးခင် super() ကို အမြဲ ခေါ်ရပါတယ်၊ constructor မရေးရင် default တစ်ခု ရပါတယ်။
- နာမည်တူ method က override ဖြစ်ပြီး super.method() နဲ့ parent ရဲ့ behavior ကို ပြန်သုံးနိုင်ပါတယ်။
- is-a ဆက်ဆံရေး တကယ်ရှိမှ inheritance ကို သုံးပြီး has-a ဆိုရင် composition ကို ရွေးပါ။
MDN — Using Classes — MDN Web Docs