ရိုးရိုးလေး ရှင်းပြမယ်
Inheritance ကို ရိုးရိုးလေးနားလည်မယ်
Inheritance က class တစ်ခုက အခြား class တစ်ခုရဲ့ properties/methods တွေကို အမွေဆက်ခံတဲ့ OOP concept ပါ။ Swift မှာ inheritance ကို class တွေပဲ support လုပ်ပါတယ်။ Struct နဲ့ enum မှာ inheritance မရှိပါ။
Problem → Why → Solution
ပြဿနာ — ဘာကြောင့် ဒီ concept လိုတာလဲ
Struct ကို inherit လုပ်လို့မရပါ။ Data model အများစုအတွက် struct + protocol ကိုအရင်စဉ်းစားပါ။
ဖြစ်လေ့ရှိတဲ့အမှား
Sample output တစ်ခုမှန်ရုံဖြင့်nil, ownership, actor isolation နဲ့error paths အားလုံးမှန်ပြီဟုယူဆခြင်း။
Core Concept — အလုပ်လုပ်ပုံကိုနားလည်မယ်
Inheritance ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Typed value
- Swift expression / function
- Safe result
Inheritance ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အဓိက Syntax / Pattern
class Notification {
func send() {
print("Sending notification...")
}
}
class EmailNotification: Notification {
override func send() {
print("Sending email notification")
}
}
let email = EmailNotification()
email.send()Inheritance ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
Code ကို အဆင့်လိုက်ဖတ်မယ်
- Typed value
class Notification {Inheritance က class တစ်ခုက အခြား class တစ်ခုရဲ့ properties/methods တွေကို အမွေဆက်ခံတဲ့ OOP concept ပါ။ Swift မှာ inheritance ကို class တွေပဲ support လုပ်ပါတယ်။ Struct နဲ့ enum မှာ inheritance မရှိပါ။
- Swift expression / function
override func send() {Inheritance ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Safe result
email.send()Inheritance example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Typed value
- Swift expression / function
- Safe result
Success
Inheritance ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
Failure
Struct ကို inherit လုပ်လို့မရပါ။ Data model အများစုအတွက် struct + protocol ကိုအရင်စဉ်းစားပါ။
Inheritance က class တစ်ခုက အခြား class တစ်ခုရဲ့ properties/methods တွေကို အမွေဆက်ခံတဲ့ OOP concept ပါ။ Swift မှာ inheritance ကို class တွေပဲ support လုပ်ပါတယ်။ Struct နဲ့ enum မှာ inheritance မရှိပါ။
Inheritance က shared behavior တွေကို reuse လုပ်ဖို့ကောင်းပေမယ့် မလိုအပ်ဘဲအလွန်အကျွံသုံးရင် code ကိုနားလည်ရခက်စေနိုင်ပါတယ်။ Swift မှာ protocol composition နဲ့ struct ကိုလည်း အများကြီးအသုံးပြုကြပါတယ်။
class Notification {
func send() {
print("Sending notification...")
}
}
class EmailNotification: Notification {
override func send() {
print("Sending email notification")
}
}
let email = EmailNotification()
email.send()EmailNotification က Notification ကို inherit လုပ်ထားပါတယ်။ Parent class ရဲ့ send() method ကို child class မှာ ကိုယ်ပိုင် behavior နဲ့ပြန်ရေးချင်လို့ override သုံးထားပါတယ်။
Sending email notificationReal-world usage
လက်တွေ့ Project မှာ ဘယ်လိုသုံးမလဲ
Inheritance example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Mini challenge
ကိုယ်တိုင်တည်ဆောက်ကြည့်ပါ
Inheritance example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
- Example ကို copy မလုပ်ဘဲ ပြန်ရေးပါ
- Normal input နဲ့ edge case စမ်းပါ
- Run မလုပ်မီ compiler/output result ကိုခန့်မှန်းပါ
class Notification {
func send() {
print("Sending notification...")
}
}
class EmailNotification: Notification {
override func send() {
print("Sending email notification")
}
}
let email = EmailNotification()
email.send()ရလဒ်: Inheritance ရဲ့type, ownership နဲ့runtime behavior ကိုရှင်းပြနိုင်ရန်
အရိပ်အမြွက် ကြည့်ရန်
Inheritance example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
ဒီသင်ခန်းစာမှာ မှတ်ထားရမယ့်အချက်များ
- Typed value → Swift expression / function → Safe result
- Inheritance ကိုstable capabilities နဲ့reusable algorithms ဖော်ပြရန်သုံးပါ။ Protocol-oriented design, constrained generics နဲ့small extensions သုံးပြီးconcrete types, inheritance နဲ့global state အပေါ်coupling ကိုလျော့ပါ။
- Struct ကို inherit လုပ်လို့မရပါ။ Data model အများစုအတွက် struct + protocol ကိုအရင်စဉ်းစားပါ။
- Inheritance example ကိုempty, nil, boundary, invalid, error နဲ့cancellation cases ဖြင့်run/test လုပ်ပါ။ Value copy, shared reference, actor isolation နဲ့main-actor effects ကိုလိုအပ်သလိုစစ်ပါ။
The Swift Programming Language — Swift.org