ရိုးရိုးလေး ရှင်းပြမယ်
Polymorphism ကို ရိုးရိုးလေးနားလည်မယ်
Polymorphism ဆိုတာ object မတူပေမယ့် method name တူကို ခေါ်တဲ့အခါ ကိုယ်ပိုင်ပုံစံနဲ့ အလုပ်လုပ်နိုင်တာပါ။ Base class မှာ virtual ထားပြီး derived class မှာ override လုပ်နိုင်ပါတယ်။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Type အလိုက်if/else အမြဲခွဲရခြင်းကmissing polymorphic contract ကိုဖော်ပြနိုင်ပါတယ်။
ဖြစ်လေ့ရှိတဲ့အမှား
Happy path တစ်ခု run အောင်ရုံဖြင့် code မှန်ကန်၊ maintainable နဲ့production-ready ဖြစ်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Polymorphism ကcommon abstraction ကိုကိုင်ထားသောcaller တစ်ခုကruntime implementation မျိုးစုံကိုသုံးနိုင်စေပါတယ်။ Virtual dispatch, interface dispatch, overload resolution နဲ့type tests တို့၏ကွာခြားချက်ကိုသိပါ။
- Object / dependency
- C# type contract + CLR
- Reusable behavior
Polymorphism ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound");
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("The dog says: woof");
}
}
class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("The cat says: meow");
}
}
class Program
{
static void Main(string[] args)
{
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.MakeSound();
myCat.MakeSound();
}
}Polymorphism ကcommon abstraction ကိုကိုင်ထားသောcaller တစ်ခုကruntime implementation မျိုးစုံကိုသုံးနိုင်စေပါတယ်။ Virtual dispatch, interface dispatch, overload resolution နဲ့type tests တို့၏ကွာခြားချက်ကိုသိပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Object / dependency
class AnimalPolymorphism ဆိုတာ object မတူပေမယ့် method name တူကို ခေါ်တဲ့အခါ ကိုယ်ပိုင်ပုံစံနဲ့ အလုပ်လုပ်နိုင်တာပါ။ Base class မှာ virtual ထားပြီး derived class မှာ override လုပ်နိုင်ပါတယ်။
- C# type contract + CLR
{Polymorphism ကcommon abstraction ကိုကိုင်ထားသောcaller တစ်ခုကruntime implementation မျိုးစုံကိုသုံးနိုင်စေပါတယ်။ Virtual dispatch, interface dispatch, overload resolution နဲ့type tests တို့၏ကွာခြားချက်ကိုသိပါ။
- Reusable behavior
}Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။
- Object / dependency
- C# type contract + CLR
- Reusable behavior
Success
Polymorphism ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Type အလိုက်if/else အမြဲခွဲရခြင်းကmissing polymorphic contract ကိုဖော်ပြနိုင်ပါတယ်။
Polymorphism ဆိုတာ object မတူပေမယ့် method name တူကို ခေါ်တဲ့အခါ ကိုယ်ပိုင်ပုံစံနဲ့ အလုပ်လုပ်နိုင်တာပါ။ Base class မှာ virtual ထားပြီး derived class မှာ override လုပ်နိုင်ပါတယ်။
class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound");
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("The dog says: woof");
}
}
class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("The cat says: meow");
}
}
class Program
{
static void Main(string[] args)
{
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.MakeSound();
myCat.MakeSound();
}
}ဘာကို သတိထားရမလဲ
Animal myDog = new Dog();ဆိုပြီး base type နဲ့ကိုင်ထားပေမယ့် တကယ် run တဲ့ method က Dog ထဲက override method ပါ။- ဒီ concept က plugin system, payment provider, notification channel လို pattern တွေမှာ အရမ်းအသုံးဝင်ပါတယ်။
The dog says: woof The cat says: meowReal-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
class Animal
{
public virtual void MakeSound()
{
Console.WriteLine("The animal makes a sound");
}
}
class Dog : Animal
{
public override void MakeSound()
{
Console.WriteLine("The dog says: woof");
}
}
class Cat : Animal
{
public override void MakeSound()
{
Console.WriteLine("The cat says: meow");
}
}
class Program
{
static void Main(string[] args)
{
Animal myDog = new Dog();
Animal myCat = new Cat();
myDog.MakeSound();
myCat.MakeSound();
}
}ရလဒ်: Polymorphism ရဲ့core concept နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Object / dependency → C# type contract + CLR → Reusable behavior
- Polymorphism ကcommon abstraction ကိုကိုင်ထားသောcaller တစ်ခုကruntime implementation မျိုးစုံကိုသုံးနိုင်စေပါတယ်။ Virtual dispatch, interface dispatch, overload resolution နဲ့type tests တို့၏ကွာခြားချက်ကိုသိပါ။
- Type အလိုက်if/else အမြဲခွဲရခြင်းကmissing polymorphic contract ကိုဖော်ပြနိုင်ပါတယ်။
- Notification interface implementation နှစ်ခုကိုlist ထဲထည့်ပြီး caller ကconcrete type မသိဘဲsend လုပ်ကြောင်းစမ်းပါ။
Object-oriented programming in C# — Microsoft Learn