Object-Oriented Programming (OOP)
Object-Oriented Programming (OOP) ဆိုတာ Code တွေကို “အရာဝတ္ထု (Objects)” တွေ အနေနဲ့ မြင်ပြီး ရေးတဲ့ ပုံစံပါ။ လက်တွေ့ကမ္ဘာက အရာတွေ (ကား၊ ဘဏ်အကောင့်၊ User Account) ကို Code အဖြစ် ပြောင်းလဲရေးသားတဲ့အခါ OOP ဟာ Code ကို ပိုမို ရှင်းလင်း ပြီး ပြန်သုံးနိုင် စေပါတယ်။
ဥပမာ — ရန်ကုန်မှာ လက်ဖက်ရည်ဆိုင် တစ်ဆိုင် ဖွင့်ချင်ရင် “Tea Shop” ဆိုတဲ့ Class တစ်ခု ဆောက်ပြီး မြို့ထဲမှာ ဆိုင်အများကြီး ဖွင့်လိုက်ပါ — Tea Shop တစ်ဆိုင်ချင်းစီဟာ Object တစ်ခုစီဖြစ်ပြီး တူညီတဲ့ Structure (menu, prices, owner) ကို inherit လုပ်ပြီး ကိုယ်ပိုင် တန်ဖိုးတွေ ထည့်သွင်းတာမျိုး ဖြစ်ပါတယ်။
Class နဲ့ Object ဆိုတာ ဘာလဲ?
Section titled “Class နဲ့ Object ဆိုတာ ဘာလဲ?”- Class — “ပုံစံကြမ်း (Blueprint)” ပါ — “ဒီပုံစံအတိုင်း Object ကို ဆောက်ပါ” ဆိုပြီး ကြေညာထားတာပါ
- Object — ပုံစံကြမ်းကို လိုက်ဆောက်ထားတဲ့ တကယ့် Instance ပါ
Class ဘယ်လို တည်ဆောက်မလဲ?
Section titled “Class ဘယ်လို တည်ဆောက်မလဲ?”class Dog: # __init__ ← Constructor: Object ဆောက်တဲ့အခါ အလိုအလျောက် Run ပါမယ် # self ← "ကိုယ်ကိုယ်တိုင်" ကို ကိုယ်စားပြုတယ် — ဒါကို မမေ့ပါနဲ့ def __init__(self, name, breed): self.name = name # Instance Attribute self.breed = breed # Instance Attribute
def bark(self): print(f"{self.name} က ဝုတ် ဝုတ် လို့ ဟောင်နေပါတယ်")Object ဖန်တီးခြင်းနဲ့ အသုံးပြုခြင်း
Section titled “Object ဖန်တီးခြင်းနဲ့ အသုံးပြုခြင်း”# Object ဆောက်ခြင်းdog1 = Dog("Aung Net", "Golden Retriever")dog2 = Dog("Phoe Wa", "Bulldog")
# Attributes တွေကို ကြည့်ခြင်းprint(dog1.name) # Aung Netprint(dog2.breed) # Bulldog
# Methods တွေကို ခေါ်ခြင်းdog1.bark() # Aung Net က ဝုတ် ဝုတ် လို့ ဟောင်နေပါတယ်dog2.bark() # Phoe Wa က ဝုတ် ဝုတ် လို့ ဟောင်နေပါတယ်
# Attribute ပြင်ခြင်းdog1.name = "Aung Net Jr."print(dog1.name) # Aung Net Jr.str — Object ကို ရှင်းလင်းစွာ ပြသခြင်း
Section titled “str — Object ကို ရှင်းလင်းစွာ ပြသခြင်း”__str__ Method မပါရင် print(dog1) ဟာ ဒီလိုမျိုး ပြပါမယ်:
<__main__.Dog object at 0x0000020B4A7A6E90>__str__ Method ထည့်လိုက်ရင်တော့ ရှင်းလင်းတဲ့ Output ပြနိုင်ပါမယ်:
class Dog: def __init__(self, name, breed, age): self.name = name self.breed = breed self.age = age
def __str__(self): return f"Dog(name='{self.name}', breed='{self.breed}', age={self.age})"
def bark(self): print(f"{self.name} က ဝုတ် ဝုတ် ဟောင်တယ်!")
dog = Dog("Lucky", "Shih Tzu", 3)print(dog) # Dog(name='Lucky', breed='Shih Tzu', age=3)Class Attributes vs Instance Attributes
Section titled “Class Attributes vs Instance Attributes”Instance Attribute — Object တစ်ခုချင်းစီမှာ ကိုယ်ပိုင် Value ရှိပါတယ် (self.name)
Class Attribute — Class ကနေ ဆောက်တဲ့ Object အားလုံး မျှဝေ သုံးတဲ့ တန်ဖိုးပါ:
class Dog: # Class Attribute — Object တိုင်းမှာ တူညီတယ် species = "Canis lupus familiaris" count = 0
def __init__(self, name): self.name = name # Instance Attribute — တစ်ကောင်ချင်းစီ ကွဲပြားမယ် Dog.count += 1 # Class Attribute ကို Class နာမည်နဲ့ ဝင်ကြည့်မယ်
@classmethod def get_count(cls): return f"ခွေး {cls.count} ကောင် ဖန်တီးပြီးပါပြီ"
d1 = Dog("Lucky")d2 = Dog("Max")d3 = Dog("Buddy")
print(Dog.species) # Canis lupus familiarisprint(d1.species) # Canis lupus familiaris (Object ကနေ ကြည့်လို့လည်း ရပါတယ်)print(Dog.get_count()) # ခွေး 3 ကောင် ဖန်တီးပြီးပါပြီInheritance (အမွေဆက်ခံခြင်း)
Section titled “Inheritance (အမွေဆက်ခံခြင်း)”ရှိပြီးသား Class ကို အခြေခံပြီး Child Class ဆောက်နိုင်ပါတယ်:
# Parent Classclass Animal: def __init__(self, name, age): self.name = name self.age = age
def __str__(self): return f"{type(self).__name__}({self.name}, {self.age} နှစ်)"
def eat(self): print(f"{self.name} က အစာ စားနေပါတယ်")
def sleep(self): print(f"{self.name} က အိပ်နေပါတယ်")
# Child Class — Animal ကနေ inherit လုပ်မယ်class Cat(Animal): def __init__(self, name, age, indoor): super().__init__(name, age) # ← Parent __init__ ကို ခေါ်ခြင်းပါ self.indoor = indoor # Cat ရဲ့ ကိုယ်ပိုင် Attribute
def meow(self): print(f"{self.name} က မြောင် လို့ ဆိုနေပါတယ်")
class Dog(Animal): def __init__(self, name, age, breed): super().__init__(name, age) self.breed = breed
def bark(self): print(f"{self.name} ({self.breed}) က ဟောင်နေပါတယ်!")
# Object ဆောက်ပြီး Test လုပ်မယ်cat = Cat("ကြောင်ဝါ", 2, indoor=True)dog = Dog("ကျား", 4, "Labrador")
# Parent Methods တွေကို Child မှာ သုံးနိုင်ပါတယ်cat.eat() # ကြောင်ဝါ က အစာ စားနေပါတယ်dog.eat() # ကျား က အစာ စားနေပါတယ်
# ကိုယ်ပိုင် Methods များကို သုံးမယ်cat.meow() # ကြောင်ဝါ က မြောင် လို့ ဆိုနေပါတယ်dog.bark() # ကျား (Labrador) က ဟောင်နေပါတယ်!
print(cat) # Cat(ကြောင်ဝါ, 2 နှစ်)print(dog) # Dog(ကျား, 4 နှစ်)Encapsulation — Private Attributes
Section titled “Encapsulation — Private Attributes”Python မှာ Convention အားဖြင့် ထိတွေ့ မသင့်တဲ့ Attribute တွေကို _ (underscore) တစ်ခုနဲ့ အစပြုရေးပြီး ညွှန်ပြပါတယ်:
class BankAccount: def __init__(self, owner, balance): self.owner = owner # Public — ပြင်ပကနေ ဝင်ကြည့်လို့ ရတယ် self._balance = balance # Private — ဒါကို တိုက်ရိုက် မပြင်ပါနဲ့
def deposit(self, amount): if amount <= 0: raise ValueError("ထည့်ငွေ ၀ ထက် ကျော်ရပါမယ်") self._balance += amount print(f"+{amount} ကျပ် ထည့်ပြီး — Remaining: {self._balance}")
def withdraw(self, amount): if amount > self._balance: raise ValueError("လက်ကျန်ငွေ မလုံလောက်ပါ") self._balance -= amount print(f"-{amount} ကျပ် ထုတ်ပြီး — Remaining: {self._balance}")
def get_balance(self): return self._balance
acc = BankAccount("Aung Aung", 10000)acc.deposit(5000) # +5000 ကျပ် ထည့်ပြီး — Remaining: 15000acc.withdraw(3000) # -3000 ကျပ် ထုတ်ပြီး — Remaining: 12000print(acc.get_balance()) # 12000Myanmar Context ဥပမာ — ကျောင်းသား စီမံခန့်ခွဲမှု
Section titled “Myanmar Context ဥပမာ — ကျောင်းသား စီမံခန့်ခွဲမှု”class Student: """ကျောင်းသားတစ်ယောက်ကို ကိုယ်စားပြုတဲ့ Class"""
school_name = "ရန်ကုန် Technological University" # Class Attribute
def __init__(self, name, roll_no, scores): self.name = name # ကျောင်းသားနာမည် self.roll_no = roll_no # ခုံနံပါတ် self.scores = scores # စာမေးပွဲ အမှတ်များ
def __str__(self): return f"Student({self.name}, Roll #{self.roll_no})"
def average(self): """ပျမ်းမျှ Score တွက်ပေးတဲ့ Method""" if not self.scores: return 0 return sum(self.scores) / len(self.scores)
def grade(self): """Grade ပြန်ပေးတဲ့ Method""" avg = self.average() if avg >= 80: return "A" elif avg >= 70: return "B" elif avg >= 60: return "C" else: return "F"
# Object ဆောက်ခြင်းmg_mg = Student("မောင်မောင်", 1001, [85, 72, 90])su_su = Student("စုစု", 1002, [92, 88, 95])
print(mg_mg) # Student(မောင်မောင်, Roll #1001)print(f"ပျမ်းမျှ: {mg_mg.average():.1f}") # ပျမ်းမျှ: 82.3print(f"Grade: {mg_mg.grade()}") # Grade: Aprint(f"ကျောင်း: {Student.school_name}")OOP ကို ဘယ်အချိန် သုံးမလဲ?
Section titled “OOP ကို ဘယ်အချိန် သုံးမလဲ?”OOP ဟာ Project ကြီးတွေ၊ Data နဲ့ Behavior တွဲနေတဲ့အရာ တွေမှာ အသုံးဝင်ပါတယ်:
| သင့်လျော် | မသင့်လျော် |
|---|---|
| User, Product, Order Model | Simple Script |
| Game Characters, Items | တစ်ကြိမ်သုံး Calculation |
| Data + Methods တွဲရမယ့်ပြဿနာ | ရိုးရှင်းတဲ့ Function တစ်ခုနဲ့ ဖြေရှင်းနိုင်တဲ့အရာ |