Classes and Objects

Class တစ်ခုကို ဘယ်လို ဆောက် မလဲ

Class ဆိုတာ ပုံစံခွက် (blueprint) လို့ ကျနော်တို့ သဘောတူထားတယ် ဟုတ်? အဲ့ဒီ ပုံစံခွက်ကို Python မှာ ဘယ်လို ဒီဇိုင်း ဆွဲမလဲ ကြည့်ရအောင်။

Keywords တွေ: class, def, __init__, self။ ဒါတွေ တွေ့ရင် မကြောင်သွားနဲ့။ တစ်ခုချင်း ရှင်းပြမယ်။

ကျနော်တို့ လူ (Person) ဆိုတဲ့ Class တစ်ခု ဆောက်ကြည့်မယ်။

Class Definition
class Person: # 1. The "Setup" Function (Constructor) def __init__(self, name, age): # 3. Attributes (Data တွေ) print(f"{name} ကို မွေးလိုက်ပြီ! Congrats!") self.name = name self.age = age self.is_awake = True # Default value # 2. Methods (အလုပ်တွေ / Functions) def say_hello(self): print(f"Hello, my name is {self.name}. ကျနော့်အသက်က {self.age} ပါ။") def sleep(self): self.is_awake = False print(f"{self.name} အိပ်သွားပြီ။ Zzz...") def wake_up(self): self.is_awake = True print(f"{self.name} နိုးလာပြီ။ Good Morning!")

အပေါ်က code ကို ခွဲစိတ် ကြည့်ရအောင်။

1. The Setup Function: __init__

Constructor Method
def __init__(self, name, age):

ဒါက magic function။ သူ့နာမည်က __init__ (ရှေ့ရောနောက်ရော underscore နှစ်ခုစီ)။

  • သူ့ကို Constructor (တည်ဆောက်သူ) လို့ ခေါ်တယ်။
  • ဘယ်အချိန်မှာ auto (အလိုအလျောက်) အလုပ်လုပ်လဲဆိုတော့... Object အသစ်တစ်ခု မွေး လိုက်တဲ့ အချိန်တိုင်း (ဥပမာ Person() လို့ခေါ်လိုက်တာနဲ့) သူက တန်း အလုပ်လုပ်တယ်။
  • သူ့အလုပ်က မွေးကတည်းက ပါလာရမယ့် data တွေ (attributes) ကို setup လုပ်ပေးဖို့ပဲ။ ဒီမှာတော့ name နဲ့ age ပေါ့။

2. Methods (အလုပ်တွေ)

Method Examples
def say_hello(self): def sleep(self):

ဒါတွေက Class ထဲမှာ ရေးထားတဲ့ function တွေ။ Class ထဲ ရောက်သွားတော့ Method လို့ နာမည်ပြောင်းခေါ်တာပဲ၊ vibe က အတူတူပဲ။

ဒါတွေက အဲ့ဒီ Object က ဘာတွေ လုပ်နိုင်လဲ ဆိုတာကို ပြောတာ။ Person က နှုတ်ဆက်နိုင်မယ် (say_hello)၊ အိပ်နိုင်မယ် (sleep)။

3. Attributes (Data တွေ)

Attribute Assignment
self.name = name self.age = age

ဒါတွေက အဲ့ဒီ Object ရဲ့ data (အချက်အလက်) တွေ။

Person တစ်ယောက်မှာ name (နာမည်) ရှိမယ်၊ age (အသက်) ရှိမယ်။

အရေးကြီးတာက: self ဆိုတာ ဘာလဲ?

ဒါက အရေးကြီးဆုံးပဲ။ bro stuck နိုင်ဆုံး နေရာ။

self ဆိုတာ: Object ကိုယ်တိုင် ကို ပြောတာ။

Class ဆိုတာ ပုံစံခွက် ပဲ ရှိသေးတယ်၊ အစစ် မဟုတ်သေးဘူး။

__init__ တို့ say_hello တို့ထဲမှာ self ကို ထည့်ရေးထားရတာက... ဟေ့ နောင်တစ်ချိန်မှာ ကျနော့်ကို ပုံတူ ထုတ်လိုက်လို့ Object အစစ် ဖြစ်လာတဲ့အခါ... အဲ့ဒီ Object အစစ် ကို self နေရာမှာ အစားထိုးပြီး သုံးလိုက် လို့ ကြိုပြောထားတာ။

self.name = name ဆိုတာ ဒီ Object ရဲ့ name ထဲကို bro ပေးလိုက်တဲ့ name ကို ထည့်လိုက် လို့ ပြောတာ။

Object တစ်ခုကို ဘယ်လို မွေး မလဲ

Ok, ပုံစံခွက် (Class) တော့ ဆောက် ပြီးပြီ။ အခု လူ အစစ် (Object) ထုတ်မယ်။

Creating Objects
# "Person" ပုံစံခွက်ကနေ Object နှစ်ယောက် မွေးမယ် person1 = Person("Aung Aung", 20) person2 = Person("Ma Mya", 22)

အဲ့ဒီ code person1 = Person("Aung Aung", 20) ကို run လိုက်တာနဲ့...

  • Python က Person class (ပုံစံခွက်) ကို ကြည့်တယ်။
  • Object အသစ် (လူ အစစ်) တစ်ယောက်ကို memory မှာ ဆောက်တယ်။
  • ပြီးတာနဲ့ __init__ function ကို auto ခေါ်တယ်။
  • self နေရာမှာ ခုနက ဆောက်လိုက်တဲ့ person1 Object အစစ်ကြီးကို အစားထိုး ထည့်ပေးတယ်။
  • name နေရာမှာ "Aung Aung" ကို ထည့်ပေးတယ်။
  • age နေရာမှာ 20 ကို ထည့်ပေးတယ်။
  • __init__ ထဲက code တွေ (self.name = name, self.age = age) run တယ်။
  • ဒါကြောင့် person1 ရဲ့ name က "Aung Aung" ဖြစ်သွားပြီး၊ person1 ရဲ့ age က 20 ဖြစ်သွားတယ်။

person2 ကို ဆောက်တဲ့ အခါလည်း အဲ့ process အတိုင်းပဲ။ self က person2 ဖြစ်သွားရော။

Note: အဲ့ဒါကြောင့် person1 နဲ့ person2 က လူ (Person) အမျိုးအစားချင်း တူပေမယ့်၊ data (name, age) တွေက တစ်ယောက်နဲ့ တစ်ယောက် မတူဘူး။

စမ်းကြည့်ရအောင်

Object ရပြီဆိုတော့... သူ့ကိုသုံးလို့ရပြီ။ dot (.) လေး သုံးပြီး ကလိရတယ်။ The dot (.) ရဲ့ အဓိပ္ပာယ်က (of).

Accessing Attributes
# Attributes တွေကို ခေါ်ကြည့်မယ် (Accessing Attributes) print(f"Person 1 ရဲ့ နာမည်က: {person1.name}") # "Aung Aung" print(f"Person 2 ရဲ့ အသက်က: {person2.age}") # 22
Calling Methods
# Methods တွေကို ခိုင်းကြည့်မယ် (Calling Methods) person1.say_hello() person2.say_hello() # ကဲ... person1 ကို အိပ်ခိုင်းမယ် person1.sleep() print(f"Aung Aung နိုးနေလား? {person1.is_awake}") # False # person2 ကတော့ နိုးတုန်း print(f"Ma Mya နိုးနေလား? {person2.is_awake}") # True

Do you notice?

person1.say_hello() လို့ ခေါ်လိုက်တဲ့အချိန် သတိထားမိလား?

  • Class ကို ဆောက် တုန်းက def say_hello(self): ဆိုပြီး self (parameter) ပါခဲ့တယ်။
  • ဒါပေမယ့် ခေါ် တဲ့ အချိန်မှာ person1.say_hello() ဆိုပြီး ကွင်းစကွင်းပိတ် () ထဲမှာ ဘာမှ ထည့်ပေးစရာ မလိုဘူး။
ဘာလို့လဲ? ဘာလို့လဲဆိုတော့... bro က person1.say_hello() (person1 ရဲ့ say_hello) လို့ ခေါ်လိုက်တာနဲ့... Python က အလိုလိုသိသွားတယ်... သြော်... say_hello function ထဲက self ဆိုတာ... person1 ကို ပြောတာပဲ ဆိုပြီး person1 ကို self နေရာမှာ auto ထည့်ပေးသွားလို့ပဲ။

ဒါပဲ... ရှင်းရှင်းလေး။

အနှစ်ချုပ်

  • Class ဆောက်: class ClassName: လို့ စတယ်။
  • Setup Function: def __init__(self, ...): က Object မွေး တိုင်း auto run တယ်။ မွေးကတည်းက ပါရမယ့် data တွေ (Attributes) ကို self.data = data ဆိုပြီး ဒီထဲမှာ သတ်မှတ်တယ်။
  • Methods ဆောက်: def method_name(self, ...): နဲ့ အဲ့ Object လုပ်နိုင်တဲ့ အလုပ် (function) တွေ ရေးတယ်။
  • Object မွေး: my_object = ClassName(...) ဆိုပြီး ပုံစံခွက်ကနေ အစစ် ထုတ်တယ်။
  • ကလိ မယ်: my_object.data (Attribute) နဲ့ my_object.method() (Method) ဆိုပြီး dot (.) နဲ့ သုံး။