What is OOP? (OOP ဆိုတာ ဘာလဲ)

B3.1: Understand the fundamental concepts of the object-oriented programming paradigm.
မြန်မာလို: object-oriented programming paradigm (object တွေကို အခြေခံပြီး program ရေးတဲ့ ပုံစံ) ရဲ့ အခြေခံ အယူအဆတွေကို နားလည်ရပါမယ်။


အဓိက အယူအဆများ

Procedural Code ရဲ့ ပြဿနာ

Procedural programming (ဒေတာနဲ့ function တွေကို သီးခြားစီ ထားပြီး အဆင့်ဆင့် ရေးတဲ့ program ရေးနည်း) မှာ သီးခြား variable (တန်ဖိုး သိမ်းထားတဲ့ နာမည်ပေးထားတဲ့ နေရာ) တွေထဲမှာ သိမ်းထားတဲ့ ဒေတာပေါ်မှာ အလုပ်လုပ်တဲ့ ညွှန်ကြားချက်တွေကို အစဉ်လိုက် ရေးပါတယ်။ Program အသေးလေးတွေအတွက် ဒါက ကောင်းကောင်း အလုပ်ဖြစ်ပေမယ့် program ကြီးလာတာနဲ့ ပြဿနာတွေ ပေါ်လာပါတယ်:

// Procedural approach: managing student data
String student1Name = "Alice";
int student1Grade = 92;
String student2Name = "Bob";
int student2Grade = 85;
String student3Name = "Charlie";
int student3Grade = 78;

// What if we need 200 students?
// What if we add email, attendance, GPA?
// What if two functions both modify the same grade variable?
# Procedural approach: managing student data
student1_name = "Alice"
student1_grade = 92
student2_name = "Bob"
student2_grade = 85
student3_name = "Charlie"
student3_grade = 78

# What if we need 200 students?
# What if we add email, attendance, GPA?
# What if two functions both modify the same grade variable?

Code ထဲက comment တွေက မေးခွန်း သုံးခု မေးထားပါတယ်: ကျောင်းသား 200 ရှိရင် ဘယ်လို လုပ်မလဲ။ Email၊ ကျောင်းတက်ရောက်မှုနဲ့ GPA (အမှတ် ပျမ်းမျှ) ထပ်ထည့်ရရင် ဘယ်လို လုပ်မလဲ။ Function နှစ်ခုလုံးက grade variable တစ်ခုတည်းကို ပြင်နေရင် ဘာဖြစ်မလဲ။

ဒေတာ (နာမည်တွေ၊ အမှတ်တွေ) နဲ့ အဲဒီ ဒေတာပေါ်မှာ လုပ်တဲ့ operation (ဒေတာပေါ်မှာ လုပ်တဲ့ လုပ်ဆောင်ချက်) တွေ၊ ဥပမာ ပျမ်းမျှ တွက်တာ၊ အောင်လား ကျလား စစ်တာတွေက သီးခြားစီ ဖြစ်နေပါတယ်။ ဘယ် function (သီးခြား အလုပ်တစ်ခု လုပ်ပေးတဲ့ code အပိုင်း၊ Java မှာ method လို့ ခေါ်ပါတယ်) မဆို ဘယ် variable ကိုမဆို ယူသုံးပြီး ပြင်လို့ ရပါတယ်။ Program ကြီးလာတာနဲ့အမျှ ဘယ် function က ဘယ်ဒေတာကို ပြောင်းလဲလဲ ဆိုတာ ခြေရာခံရ ခက်လာပြီး bug (code ထဲက အမှား) တွေကိုလည်း ရှာရ ခက်လာပါတယ်။

OOP ရဲ့ ဖြေရှင်းနည်း

Object-oriented programming (OOP) ဆိုတာ object တွေကို အခြေခံပြီး program ရေးနည်းပါ။ OOP က ဆက်စပ်နေတဲ့ ဒေတာနဲ့ အဲဒီ ဒေတာပေါ်မှာ လုပ်တဲ့ operation တွေကို object (class တစ်ခုကနေ ဖန်တီးထားတဲ့ instance) လို့ ခေါ်တဲ့ ကိုယ့်ဘာသာ ပြည့်စုံတဲ့ အစိတ်အပိုင်းတွေထဲကို တစ်စုတည်း စုထုပ်ပြီး ဒီပြဿနာကို ဖြေရှင်းပါတယ်။ Object တစ်ခုစီက ကိုယ့်ရဲ့ state (object ရဲ့ attribute တွေမှာ လက်ရှိ ရှိနေတဲ့ တန်ဖိုးများ) ကို ကိုယ်တိုင် စီမံရပါတယ်။

// OOP approach: each student is a self-contained object
Student alice = new Student("Alice", 92);
Student bob = new Student("Bob", 85);

alice.getGrade();      // only Alice's object knows Alice's grade
bob.updateGrade(90);   // only Bob's object can change Bob's grade
# OOP approach: each student is a self-contained object
alice = Student("Alice", 92)     # Python has no "new" keyword - calling the class creates the object
bob = Student("Bob", 85)

alice.get_grade()      # only Alice's object knows Alice's grade
bob.update_grade(90)   # only Bob's object can change Bob's grade

Comment တွေက Alice ရဲ့ အမှတ်ကို Alice ရဲ့ object ကပဲ သိပြီး Bob ရဲ့ အမှတ်ကို Bob ရဲ့ object ကပဲ ပြောင်းနိုင်တယ်လို့ ဆိုလိုပါတယ်။ Python မှာ new keyword (programming language မှာ အထူး အဓိပ္ပာယ်ရှိတဲ့ စကားလုံး) မရှိပါဘူး: class ကို ခေါ်လိုက်တာနဲ့ object ကို ဖန်တီးပြီးသား ဖြစ်ပါတယ်။

အခု ဒေတာနဲ့ operation တွေက အတူတူ ရှိနေပါပြီ။ Object တစ်ခုစီက ကိုယ့်ဒေတာကို ကိုယ်တိုင် ကာကွယ်ထားလို့ Bob ရဲ့ အမှတ်ကို ပြောင်းမလို့ လုပ်ရင်း Alice ရဲ့ အမှတ်ကို မတော်တဆ ပြောင်းမိတာ မဖြစ်နိုင်ပါဘူး။

OOP ရဲ့ အခြေခံ သဘောတရား လေးခု

OOP ကို အခြေခံ သဘောတရား (principle) လေးခုပေါ်မှာ တည်ဆောက်ထားပါတယ်။ လေးခုလုံးကို ချက်ချင်း ကျွမ်းကျင်အောင် လုပ်ဖို့ မလိုပါဘူး: ဒီစာမျက်နှာမှာ မိတ်ဆက်ပေးပြီး နောက်စာမျက်နှာတွေမှာ တစ်ခုချင်းစီကို အသေးစိတ် လေ့လာပါမယ်။

1. Encapsulation (ဒေတာနဲ့ method ကို တစ်စုတည်း ထုပ်ပိုးခြင်း)

Encapsulation ဆိုတာ ဒေတာ၊ ဆိုလိုတာက attribute (object ရဲ့ ဒေတာကို သိမ်းတဲ့ variable) တွေနဲ့ အဲဒီ ဒေတာပေါ်မှာ အလုပ်လုပ်တဲ့ method (သီးခြား အလုပ်တစ်ခု လုပ်ပေးတဲ့ code အပိုင်း) တွေကို unit တစ်ခုတည်း၊ ဆိုလိုတာက class (object တွေ ဖန်တီးဖို့ ပုံစံခွက်) ထဲမှာ စုထုပ်ပြီး ဒေတာကို တိုက်ရိုက် ယူသုံးခွင့်ကို ကန့်သတ်တာပါ။ ဒေတာကို private (class ပြင်ပကနေ တိုက်ရိုက် သုံးလို့ မရ) အဖြစ် ထားပြီး public (ဘယ်ကနေမဆို သုံးလို့ရ) method တွေကနေတဆင့်ပဲ ယူသုံးလို့ ရပါတယ်။

ဘဏ်အကောင့် တစ်ခုကို စဉ်းစားကြည့်ပါ: ငွေတိုက်ထဲကို ကိုယ်တိုင် လက်လှမ်းပြီး ငွေ ယူလို့ မရပါဘူး။ ငွေကိုင် ဝန်ထမ်း (method တစ်ခု) ကနေတဆင့် သွားရပါတယ်။ အဲဒီ ဝန်ထမ်းက ငွေ ထုတ်တာ၊ သွင်းတာကို မလုပ်ခင် ကိုယ့်ရဲ့ အထောက်အထားနဲ့ လက်ကျန်ငွေကို စစ်ပါတယ်။

ဘာကြောင့် အရေးကြီးလဲ: ဒေတာကို မတော်တဆ ဒါမှမဟုတ် ခွင့်ပြုချက် မရှိဘဲ ပြင်တာကို ကာကွယ်ပေးပါတယ်၊ ဘယ်တန်ဖိုးတွေ မှန်ကန်လဲ ဆိုတာကို class ကပဲ ထိန်းချုပ်ပါတယ်။

2. Inheritance (parent class ကနေ အမွေဆက်ခံခြင်း)

Inheritance က class အသစ် တစ်ခုကို ရှိပြီးသား class တစ်ခုရဲ့ attribute တွေနဲ့ method တွေကို ပြန်သုံးပြီး ထပ်တိုးချဲ့ခွင့် ပေးပါတယ်။ Class အသစ်ကို child class ဒါမှမဟုတ် subclass (အမွေဆက်ခံတဲ့ child class) လို့ ခေါ်ပြီး ရှိပြီးသား class ကို parent class ဒါမှမဟုတ် superclass (အမွေပေးတဲ့ parent class) လို့ ခေါ်ပါတယ်။ Child class က parent class ဆီကနေ အရာအားလုံးကို အမွေဆက်ခံပြီး behaviour (object က လုပ်နိုင်တာတွေ) အသစ်တွေ ထပ်ထည့်တာ ဒါမှမဟုတ် ရှိပြီးသား behaviour ကို ပြင်တာ လုပ်နိုင်ပါတယ်။

Hierarchy (အထက်အောက် အဆင့်ဆင့် ဖွဲ့စည်းပုံ) တစ်ခုကို စဉ်းစားကြည့်ပါ: ယာဉ်အားလုံးမှာ တူညီတဲ့ ဂုဏ်သတ္တိ တချို့ (မြန်နှုန်း၊ လောင်စာ) ရှိပါတယ်၊ ဒါပေမယ့် ကားမှာ တံခါးတွေ ရှိပြီး ဆိုင်ကယ်မှာ မရှိပါဘူး။ မျှသုံးတဲ့ ဂုဏ်သတ္တိတွေကို ပြန်ရေးမယ့်အစား car class နဲ့ motorcycle class တို့က vehicle class တစ်ခုတည်းကနေ အမွေဆက်ခံပါတယ်။

ဘာကြောင့် အရေးကြီးလဲ: code ကို ပြန်သုံးနိုင်စေပါတယ်၊ ထပ်ခါထပ်ခါ ရေးရတာကို လျှော့ပေးပါတယ်၊ အဓိပ္ပာယ်ရှိတဲ့ hierarchy တွေကို ဖန်တီးပေးပါတယ်။

3. Polymorphism (ပုံစံ အမျိုးမျိုး ယူနိုင်ခြင်း)

Polymorphism ဆိုတာ မတူတဲ့ object တွေက method call (method ကို ခေါ်သုံးခြင်း) တစ်ခုတည်းကို မတူတဲ့ ပုံစံနဲ့ တုံ့ပြန်နိုင်တာပါ။ Dog ရော Cat မှာပါ speak() method ရှိရင် ခွေးပေါ်မှာ speak() ကို ခေါ်ရင် “Woof” ထွက်ပြီး ကြောင်ပေါ်မှာ ခေါ်ရင် “Meow” ထွက်ပါတယ်: method နာမည် တူပေမယ့် behaviour မတူပါဘူး။

ဘာကြောင့် အရေးကြီးလဲ: compile time (code ကို compile လုပ်တဲ့ အချိန်) မှာ တိကျတဲ့ type (Dog၊ Cat) ကို မသိဘဲ ယေဘုယျ type (Animal) နဲ့ အလုပ်လုပ်တဲ့ code ကို ရေးနိုင်စေပါတယ်။

4. Abstraction (မလိုတဲ့ အသေးစိတ်ကို ဖယ်ပြီး အဓိကကိုပဲ ကြည့်ခြင်း)

Abstraction ဆိုတာ ရှုပ်ထွေးတဲ့ implementation (တကယ် အလုပ်လုပ်အောင် ရေးထားတဲ့ code) အသေးစိတ်တွေကို ဖုံးထားပြီး လိုအပ်တာကိုပဲ ပြထားတာပါ။ ကား မောင်းတဲ့အခါ စတီယာရင်နဲ့ ခြေနင်းတွေကို သုံးပါတယ်: အတွင်းလောင်ကျွမ်း အင်ဂျင် ဘယ်လို အလုပ်လုပ်လဲ နားလည်ဖို့ မလိုပါဘူး။ ကားက ရှုပ်ထွေးမှုကို ဖုံးကွယ်ပေးထားပါတယ်။

ဘာကြောင့် အရေးကြီးလဲ: program ရဲ့ တခြား အပိုင်းတွေက object တစ်ခုနဲ့ ဆက်သွယ်ပုံကို ရိုးရှင်းစေပါတယ်၊ တစ်ပြိုင်နက် စဉ်းစား မှတ်ထားရတဲ့ ဝန်ကို လျှော့ပေးပါတယ်။

ဒီ သဘောတရား လေးခုက အတူတူ အလုပ်လုပ်ပါတယ်။ Encapsulation က ဒေတာကို ဖုံးထားပါတယ်။ Inheritance က code ကို ပြန်သုံးပါတယ်။ Polymorphism က code ကို ပြောင်းလွယ်ပြင်လွယ် ဖြစ်စေပါတယ်။ Abstraction က အရာတွေကို ရိုးရှင်းအောင် ထားပေးပါတယ်။ ကောင်းကောင်း ဒီဇိုင်းဆွဲထားတဲ့ OOP program တစ်ခုက လေးခုလုံးကို သုံးပါတယ်။

အဓိက ဝေါဟာရများ

ဒီ term တွေက OOP တစ်လျှောက်လုံးမှာ ပါသလို IB Computer Science (နိုင်ငံတကာ ကျောင်းတွေမှာ သင်တဲ့ ကွန်ပျူတာသိပ္ပံ သင်ရိုး) ရဲ့ စာမေးပွဲတွေမှာလည်း ပါပါတယ်။ တိတိကျကျ မှတ်ထားပါ။

Term အဓိပ္ပာယ်
Class Object တစ်ခုမှာ ရှိမယ့် attribute တွေနဲ့ method တွေကို သတ်မှတ်ပေးတဲ့ blueprint (တည်ဆောက်ဖို့ ပုံစံ ပုံကြမ်း)
Object Class တစ်ခုကနေ ဖန်တီးထားတဲ့ သီးခြား instance တစ်ခု
Attribute Object တစ်ခု ပိုင်တဲ့ variable (field ဒါမှမဟုတ် instance variable လို့လည်း ခေါ်ပါတယ်)
Method Object တစ်ခု ပိုင်ပြီး အဲဒီ object ရဲ့ attribute တွေကို ယူသုံးနိုင်တဲ့ function
Instantiation new keyword ကို သုံးပြီး class တစ်ခုကနေ object အသစ် ဖန်တီးတဲ့ လုပ်ငန်းစဉ် (Java မှာပါ၊ Python မှာ new မရှိပါဘူး: class ကို function လို ခေါ်လိုက်ရုံပါပဲ)
Constructor Instantiation လုပ်တဲ့အချိန်မှာ ခေါ်ပြီး object ရဲ့ attribute တွေကို စတင် တန်ဖိုး သတ်မှတ်ပေးတဲ့ အထူး method
State သတ်မှတ်ထားတဲ့ အချိန် တစ်ခုမှာ object တစ်ခုရဲ့ attribute အားလုံးမှာ ရှိနေတဲ့ လက်ရှိ တန်ဖိုးများ
Behaviour Object တစ်ခု လုပ်နိုင်တာတွေ: သူ့ရဲ့ method တွေက သတ်မှတ်ပေးပါတယ်

OOP ကို ဘယ်အချိန်မှာ သုံးမလဲ

OOP က အမြဲတမ်း အကောင်းဆုံး နည်းလမ်း မဟုတ်ပါဘူး။ ဒီလို အခြေအနေတွေမှာ အထူး အသုံးဝင်ပါတယ်:

  • လက်တွေ့ ကမ္ဘာက အရာတွေ (ကျောင်းသားတွေ၊ အကောင့်တွေ၊ ကုန်ပစ္စည်းတွေ၊ ယာဉ်တွေ) ကို ပုံဖော်နေတဲ့အခါ
  • အမျိုးအစားတူ object အများ တစ်ပြိုင်နက် ရှိနေဖို့ လိုတဲ့အခါ
  • ဒေတာနဲ့ အဲဒီ ဒေတာပေါ်မှာ လုပ်တဲ့ operation တွေ အနီးကပ် ဆက်စပ်နေတဲ့အခါ
  • Code ကို စနစ်တကျ စီစဉ်ဖို့ အရေးကြီးလောက်အောင် program ကြီးတဲ့အခါ

ရိုးရှင်းတဲ့ script (code တိုတို) တွေ ဒါမှမဟုတ် တစ်ခါသုံး တွက်ချက်မှုတွေအတွက်တော့ procedural code က ရေးရတာ ပိုရိုးရှင်းပြီး ပိုမြန်လေ့ ရှိပါတယ်။


အဆင့်ဆင့် ရှင်းပြထားတဲ့ ဥပမာများ

ဥပမာ 1: Procedural နဲ့ OOP စဉ်းစားပုံ

အခြေအနေ: တိရစ္ဆာန်ဆေးခန်း တစ်ခုက လူနာ (တိရစ္ဆာန်) တွေရဲ့ မှတ်တမ်းကို ထိန်းသိမ်းပါတယ်။ တိရစ္ဆာန် တစ်ကောင်စီမှာ နာမည်၊ မျိုးစိတ်၊ အသက်နဲ့ ကာကွယ်ဆေး ထိုးပြီး မထိုးရသေး အခြေအနေ ရှိပါတယ်။

Procedural နည်း:

String[] names = {"Max", "Bella", "Luna"};
String[] species = {"Dog", "Cat", "Dog"};
int[] ages = {3, 5, 2};
boolean[] vaccinated = {true, false, true};

// To check if Bella is vaccinated, you need to know she is at index 1
// across ALL four arrays. If arrays get out of sync, data is corrupted.
names = ["Max", "Bella", "Luna"]
species = ["Dog", "Cat", "Dog"]
ages = [3, 5, 2]
vaccinated = [True, False, True]

# To check if Bella is vaccinated, you need to know she is at index 1
# across ALL four lists. If lists get out of sync, data is corrupted.

Comment က Bella ကာကွယ်ဆေး ထိုးပြီးလား စစ်ဖို့ သူက array (အရွယ်အစား ပုံသေ၊ အမျိုးအစားတူ ဒေတာစု) လေးခုလုံးမှာ index (နေရာအမှတ်) 1 မှာ ရှိတယ်ဆိုတာ သိထားရမယ်လို့ ဆိုပါတယ်။ Array တွေရဲ့ အစဉ် တစ်ခုနဲ့ တစ်ခု မကိုက်တော့ရင် ဒေတာ မှားကုန်ပါပြီ။

OOP နည်း:

public class Animal {
    private String name;
    private String species;
    private int age;
    private boolean vaccinated;

    public Animal(String name, String species, int age) {
        this.name = name;
        this.species = species;
        this.age = age;
        this.vaccinated = false;
    }

    public void vaccinate() { this.vaccinated = true; }
    public boolean isVaccinated() { return vaccinated; }
    public String getName() { return name; }
}
class Animal():
    def __init__(self, name, species, age):
        self._name = name          # the leading underscore marks the attribute as private (IB convention)
        self._species = species
        self._age = age
        self._vaccinated = False

    def vaccinate(self):
        self._vaccinated = True

    def is_vaccinated(self):
        return self._vaccinated

    def get_name(self):
        return self._name
Animal bella = new Animal("Bella", "Cat", 5);
bella.vaccinate();
System.out.println(bella.getName() + " vaccinated: " + bella.isVaccinated());
bella = Animal("Bella", "Cat", 5)
bella.vaccinate()
print(bella.get_name(), "vaccinated:", bella.is_vaccinated())
# Python prints the boolean as "True" (capital T); Java prints "true"

Python code မှာ attribute နာမည် ရှေ့က underscore (_) က အဲဒီ attribute ကို private အဖြစ် သတ်မှတ်ကြောင်း ပြတာပါ (IB က သုံးတဲ့ ရေးထုံးပါ)။ Python က boolean (true ဒါမှမဟုတ် false တန်ဖိုးပဲ ယူတဲ့) တန်ဖိုးကို True (T အကြီး) လို့ print ထုတ်ပြီး Java က true လို့ ထုတ်ပါတယ်။

Output (program က ထုတ်ပြတဲ့ ရလဒ်):

Bella vaccinated: true

တိရစ္ဆာန် တစ်ကောင်စီရဲ့ ဒေတာက unit တစ်ခုတည်းအဖြစ် အတူတူ ရှိနေပါတယ်။ Object တစ်ခုစီက ကိုယ့် state ကို ကိုယ်တိုင် စီမံလို့ Bella ရဲ့ ကာကွယ်ဆေး မှတ်တမ်းကို Max ဆီ မတော်တဆ ပေးမိတာ မဖြစ်နိုင်ပါဘူး။


ဥပမာ 2: သဘောတရား လေးခုကို ခွဲခြားသိခြင်း

အခြေအနေ တစ်ခုစီမှာ ဘယ် OOP သဘောတရားကို ပြနေလဲ ခွဲခြားကြည့်ပါ:

အခြေအနေ သဘောတရား ဘာကြောင့်လဲ
Recipe class က servings ကို private အဖြစ် ထားပြီး တန်ဖိုးကို စစ်ပေးတဲ့ setServings() ကနေတဆင့်ပဲ ပြောင်းခွင့် ပေးပါတယ် Encapsulation ဒေတာကို ဖုံးထားပြီး method တွေကနေတဆင့် ယူသုံးခွင့်ကို ထိန်းချုပ်ထားပါတယ်
Teacher class က SchoolMember ကို extend (အမွေဆက်ခံပြီး ချဲ့ထွင်) လုပ်ပြီး getSubject() method ကို ထပ်ထည့်ပါတယ် Inheritance Class အသစ်က ရှိပြီးသား class ကို ပြန်သုံးပြီး behaviour အသစ် ထပ်ထည့်ပါတယ်
Dog.speak() ရော Cat.speak() ပါ ရှိပေမယ့် output မတူပါဘူး Polymorphism Method နာမည် တူပေမယ့် object ပေါ် မူတည်ပြီး behaviour ကွာပါတယ်
Car class က drive() နဲ့ brake() ကို ပြထားပေမယ့် အင်ဂျင်ကို ထိန်းချုပ်တဲ့ logic ကို ဖုံးထားပါတယ် Abstraction ရှုပ်ထွေးတဲ့ အတွင်းပိုင်းကို ဖုံးထားပြီး မရှိမဖြစ် လိုတဲ့ interface (တခြား code က သုံးလို့ရတဲ့ public method များ) ကိုပဲ ပြထားပါတယ်

Quick Check (အမြန် စစ်ဆေးမေးခွန်း)

Q1. Object-oriented programming ရဲ့ အဓိက အယူအဆက ဘာလဲ။

Q2. ဒေတာကို private လုပ်ပြီး အဲဒီ ဒေတာကို ယူသုံးဖို့ public method တွေ ပေးတာက ဘယ် OOP သဘောတရားလဲ။

Q3. Instantiation ဆိုတာ ဘာလဲ။

Q4. Car class နဲ့ Motorcycle class နှစ်ခုလုံးက speed နဲ့ fuel လို attribute တွေကို Vehicle class ကနေ မျှသုံးပါတယ်။ ဒါက ဘယ် သဘောတရားလဲ။

Q5. OOP မှာ "state" ဆိုတဲ့ term က ဘာကို ဆိုလိုလဲ။


Fill in the Blanks (ကွက်လပ် ဖြည့်ခြင်း)

ကွက်လပ်ပါတဲ့ စာကြောင်းတွေက English နဲ့ ဖြစ်ပြီး ကွက်လပ်တွေကိုလည်း English term နဲ့ ရိုက်ထည့်ရပါမယ်။ စာကြောင်း တစ်ကြောင်းချင်းရဲ့ အဓိပ္ပာယ်က အောက်ပါအတိုင်းပါ:

  • …… ဆိုတာ attribute နဲ့ method တွေကို သတ်မှတ်ပေးတဲ့ blueprint ပါ။
  • …… ဆိုတာ အဲဒီ blueprint ကနေ ဖန်တီးထားတဲ့ သီးခြား instance တစ်ခုပါ။
  • …… က ဒေတာနဲ့ method တွေကို စုထုပ်ပြီး တိုက်ရိုက် ယူသုံးခွင့်ကို ကန့်သတ်ပါတယ်။
  • …… က class အသစ် တစ်ခုကို ရှိပြီးသား class ကို ပြန်သုံးပြီး ချဲ့ထွင်ခွင့် ပေးပါတယ်။
  • …… ဆိုတာ မတူတဲ့ object တွေက method တစ်ခုတည်းကို မတူအောင် တုံ့ပြန်တာပါ။
  • Object ဖန်တီးတဲ့ လုပ်ငန်းစဉ်ကို …… လို့ ခေါ်ပါတယ်။

OOP ဝေါဟာရ အဓိပ္ပာယ်ဖွင့်ဆိုချက်တွေကို ပြည့်စုံအောင် ဖြည့်ပါ:

A  is a blueprint that defines attributes and methods.

An  is a specific instance created from that blueprint.

 bundles data and methods while restricting direct access.

 allows a new class to reuse and extend an existing class.

 means different objects respond differently to the same method.

The process of creating an object is called .

Practice Exercises (လေ့ကျင့်ခန်းများ)

Core (အခြေခံ အဆင့်)

  1. ကိုယ့်စကားနဲ့ Define (အဓိပ္ပာယ်ကို တိတိကျကျ ဖော်ပြပါ) လုပ်ပါ: OOP သဘောတရား လေးခု (encapsulation၊ inheritance၊ polymorphism၊ abstraction) တစ်ခုချင်းစီရဲ့ အဓိပ္ပာယ်ကို စာကြောင်း တစ်ကြောင်းစီနဲ့ ရေးပါ။ တစ်ခုချင်းစီအတွက် ဒီစာမျက်နှာမှာ မပါတဲ့ လက်တွေ့ ဘဝက နှိုင်းယှဉ်ချက် ဥပမာ တစ်ခုစီ ပေးပါ။

  2. Procedural နဲ့ OOP: သီချင်း နားထောင်တဲ့ streaming app တစ်ခုက သီချင်းတွေရဲ့ မှတ်တမ်းကို ထိန်းဖို့ လိုပါတယ်။ သီချင်း တစ်ပုဒ်စီမှာ ခေါင်းစဉ်၊ အဆိုတော်၊ ကြာချိန် (စက္ကန့်) နဲ့ ဖွင့်ခဲ့တဲ့ အကြိမ်ရေ ရှိပါတယ်။ (a) parallel arrays (index တူတဲ့ နေရာတွေမှာ ဆက်စပ်တဲ့ ဒေတာ သိမ်းထားတဲ့ array များ) (procedural) နဲ့ (b) Song class (OOP) ကို သုံးပြီး ဒါကို ဘယ်လို ပုံဖော်မလဲ ဆိုတာ Describe (ဖော်ပြရှင်းပြပါ) လုပ်ပါ။ OOP နည်းက ဘယ်ပြဿနာကို ရှောင်ပေးလဲ။

  3. ဝေါဟာရ တွဲဖက်ခြင်း: term တစ်ခုချင်းစီကို သူ့ အဓိပ္ပာယ်နဲ့ တွဲပါ: class၊ object၊ attribute၊ method၊ constructor (object ဖန်တီးချိန် attribute တွေကို စတင် သတ်မှတ်ပေးတဲ့ method)၊ state၊ instantiation (class ကနေ object အသစ် ဖန်တီးခြင်း)။

Extension (တိုးချဲ့ အဆင့်)

  1. သဘောတရားကို Identify (ဖြစ်နိုင်တဲ့ အဖြေတွေထဲက ရွေးပြီး ဖော်ပြပါ) လုပ်ပါ: အခြေအနေ တစ်ခုစီမှာ သုံးထားတဲ့ OOP သဘောတရားရဲ့ နာမည်ကို ပြောပြီး ဘာကြောင့်လဲ Explain (ရှင်းပြပါ) လုပ်ပါ:
    • (a) Rectangle class က သူ့ရဲ့ width နဲ့ height ကို ဖုံးထားပြီး validation (တန်ဖိုး မှန်မမှန် စစ်ခြင်း) ပါတဲ့ setWidth() ကနေတဆင့်ပဲ ပြောင်းခွင့် ပေးပါတယ်
    • (b) Shape class မှာ draw() method ရှိပြီး Circle၊ Square နဲ့ Triangle တစ်ခုစီက မတူအောင် ဆွဲဖို့ အဲဒီ method ကို override (parent ရဲ့ method ကို child က အစားထိုး ရေးခြင်း) လုပ်ပါတယ်
    • (c) ElectricCar class က Car class ကို extend လုပ်ပြီး speed နဲ့ fuel ကို အမွေဆက်ခံပါတယ်၊ ဒါပေမယ့် batteryLevel ကို ထပ်ထည့်ပါတယ်

Challenge (စိန်ခေါ်မှု အဆင့်)

  1. ဒီဇိုင်း စဉ်းစားခြင်း: ရေခဲမုန့်ဆိုင် တစ်ဆိုင်အတွက် order မှာတဲ့ screen ကို ဒီဇိုင်းဆွဲနေတယ်လို့ ယူဆပါ။ Customer တစ်ယောက်ရဲ့ order တစ်ခုမှာ serving (တစ်ယောက်စာ ရေခဲမုန့် တစ်ခု) တစ်ခု ဒါမှမဟုတ် တစ်ခုထက် ပိုပါပါတယ်။ Serving တစ်ခုစီက cone (ရေခဲမုန့် ကတော့) ဖြစ်နိုင်ပြီး scoop (ဇွန်းနဲ့ ခပ်ထည့်တဲ့ ရေခဲမုန့် တစ်လုံး) သုံးလုံးအထိ ဆံ့ပါတယ်၊ ဒါမှမဟုတ် cup (ခွက်) ဖြစ်နိုင်ပြီး လေးလုံးအထိ ဆံ့ပါတယ်။ Scoop တစ်လုံးစီက အရသာ တစ်မျိုးစီ ဖြစ်ပြီး အရသာ တစ်မျိုးစီမှာ နာမည်နဲ့ scoop တစ်လုံးချင်းရဲ့ ဈေးနှုန်း ရှိပါတယ်။ Cone က ကတော့ကိုယ်တိုင်အတွက် ပုံသေ ကျသင့်ငွေ တစ်ခု ထပ်ပေါင်းပါတယ်၊ cup ကတော့ ဘာမှ ထပ်မပေါင်းပါဘူး။ Screen က serving တစ်ခုစီရဲ့ ဈေးနှုန်းနဲ့ order ရဲ့ စုစုပေါင်းကို ပြပေးရပြီး serving တစ်ခုထဲကို ဆံ့တာထက် ပိုတဲ့ scoop တွေ ထည့်ခွင့် မပေးရပါဘူး။
    • (a) ဖန်တီးမယ့် class အနည်းဆုံး 3 ခုကို Identify လုပ်ပါ
    • (b) Class တစ်ခုစီအတွက် attribute တွေနဲ့ method တွေကို စာရင်းပြုစုပါ
    • (c) ဘယ် OOP သဘောတရားတွေကို ဘယ်နေရာမှာ သုံးမလဲ Identify လုပ်ပါ
    • (d) Class တွေကြားက ဆက်နွယ်မှုတွေကို Identify လုပ်ပါ: has-a (object တစ်ခုထဲမှာ နောက် object တစ်ခု ပါဝင်တဲ့ ဆက်နွယ်မှု) ဒါမှမဟုတ် is-a (class တစ်ခုက နောက် class တစ်ခုရဲ့ အမျိုးအစားခွဲ ဖြစ်တဲ့ ဆက်နွယ်မှု)

ဆက်စပ် ခေါင်းစဉ်များ

  • အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: Programming Constructs (Program တည်ဆောက်တဲ့ အခြေခံ ပုံစံများ): class ထဲမှာ method တွေ မရေးခင် method ကြေညာပုံ၊ parameter (method ကို ပေးလိုက်တဲ့ တန်ဖိုးကို လက်ခံတဲ့ variable) တွေနဲ့ return value (ပြန်ပေးတဲ့ တန်ဖိုး) တွေကို နားလည်ထားဖို့ မရှိမဖြစ် လိုပါတယ်
  • အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: 1D Arrays (တန်းတစ်တန်းတည်းမှာ စီထားတဲ့ ဒေတာအစုများ): object တွေရဲ့ array တွေကလည်း ပုံစံ အတူတူပါပဲ
  • နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်: Classes and Objects (class နဲ့ object များ): ဒီ အယူအဆတွေကို Java code အဖြစ် ရေးကြည့်ခြင်း
  • နောက်ပိုင်းမှာ: Encapsulation: အသေးစိတ် လေ့လာမယ့် ပထမဆုံး သဘောတရား

© EduCS.me. ကွန်ပျူတာသိပ္ပံ ပညာရေးအတွက် အရင်းအမြစ်တွေ စုထားတဲ့ နေရာပါ။ EduCS is an independent educational resource. It is not affiliated with, or endorsed by, the International Baccalaureate Organization. (EduCS ဟာ သီးခြား ပညာရေး အရင်းအမြစ်တစ်ခုပါ။ International Baccalaureate Organization နဲ့ ဆက်စပ်မှု မရှိသလို သူတို့ရဲ့ ထောက်ခံချက်လည်း မရှိပါဘူး။) ဆက်သွယ်ရန်