Constructors (object ဖန်တီးချိန် attribute တွေကို စတင် သတ်မှတ်ပေးတဲ့ method များ)
B3.1: Design and implement constructors to initialise objects.
မြန်မာလို: object (class တစ်ခုကနေ ဖန်တီးထားတဲ့ instance) တွေကို စတင် တန်ဖိုး သတ်မှတ်ပေးဖို့ constructor တွေကို ဒီဇိုင်းဆွဲပြီး ရေးနိုင်ရပါမယ်။
အဓိက အယူအဆများ
Constructor ဆိုတာ ဘာလဲ
Constructor ဆိုတာ new keyword (programming language မှာ အထူး အဓိပ္ပာယ်ရှိတဲ့ စကားလုံး) နဲ့ object အသစ် ဖန်တီးတဲ့အခါ အလိုအလျောက် ခေါ်တဲ့ အထူး method (သီးခြား အလုပ်တစ်ခု လုပ်ပေးတဲ့ code အပိုင်း) တစ်ခုပါ။ သူ့ရည်ရွယ်ချက်က object ရဲ့ attribute (object ရဲ့ ဒေတာကို သိမ်းတဲ့ variable) တွေကို စတင် တန်ဖိုး သတ်မှတ်ဖို့ ပါ: object စ ရှိလာတဲ့ အချိန်ကတည်းက မှန်ကန်ပြီး သုံးလို့ရတဲ့ state (object ရဲ့ attribute တွေမှာ လက်ရှိ ရှိနေတဲ့ တန်ဖိုးများ) မှာ ရှိနေအောင် attribute တွေကို သူတို့ရဲ့ စတင် တန်ဖိုးတွေ ထည့်ပေးတာပါ။
Constructor တွေကို သာမန် method တွေနဲ့ ခွဲခြားပေးတဲ့ စည်းမျဉ်း သုံးခု ရှိပါတယ်:
- Constructor ရဲ့ နာမည်က class (object တွေ ဖန်တီးဖို့ ပုံစံခွက်) နာမည်နဲ့ အတိအကျ တူရပါမယ်
- Constructor တွေမှာ return type (method က ပြန်ပေးတဲ့ ဒေတာ အမျိုးအစား) မရှိပါဘူး၊
voidတောင် မရှိပါဘူး - Constructor ကို object တစ်ခုအတွက် တစ်ကြိမ်ပဲ ခေါ်ပါတယ်၊ object ကို ဖန်တီးတဲ့ အချိန်မှာပါ
public class Pet {
private String name;
private String species;
private int age;
// Constructor -- same name as the class, no return type
public Pet(String name, String species, int age) {
this.name = name;
this.species = species;
this.age = age;
}
}
class Pet():
# In Python the constructor is ALWAYS named __init__ -
# it never matches the class name like Java's does
def __init__(self, name, species, age):
self._name = name
self._species = species
self._age = age
Python မှာ constructor ရဲ့ နာမည်က အမြဲ __init__ ဖြစ်ပါတယ်: Java လို class နာမည်နဲ့ ဘယ်တော့မှ မတူပါဘူး။
// The constructor runs automatically when new is called
Pet dog = new Pet("Max", "Dog", 3);
// At this point, dog.name is "Max", dog.species is "Dog", dog.age is 3
# The constructor runs automatically when the class is called - no "new" keyword
dog = Pet("Max", "Dog", 3)
# At this point, dog's name is "Max", species is "Dog", age is 3
new ကို ခေါ်တာနဲ့ constructor က အလိုအလျောက် run ပါတယ် (Python မှာတော့ new မရှိလို့ class ကို ခေါ်တာနဲ့ပါ)။ အဲဒီ line ပြီးတဲ့အခါ dog ရဲ့ name က "Max"၊ species က "Dog"၊ age က 3 ဖြစ်နေပါပြီ။
Constructor က class တစ်ခုရဲ့ instance (class တစ်ခုကနေ ဖန်တီးထားတဲ့ object) တစ်ခုကို ဖန်တီးပြီး သူ့ရဲ့ instance variable (object တစ်ခုချင်းရဲ့ ကိုယ်ပိုင် variable) တွေကို တန်ဖိုး ထည့်ပေးပါတယ်။ ဒီ အဓိပ္ပာယ်ဖွင့်ဆိုချက်ကို စကားလုံး မလွဲ မှတ်ထားပါ။ English လို: A constructor creates an instance of a class and assigns values to its instance variables.
Default Constructor (parameter မယူဘဲ default တန်ဖိုးတွေ ထည့်ပေးတဲ့ constructor)
Default constructor က parameter (method ကို ပေးလိုက်တဲ့ တန်ဖိုးကို လက်ခံတဲ့ variable) မယူဘဲ attribute အားလုံးကို default value (သတ်မှတ်မထားရင် သုံးတဲ့ တန်ဖိုး) တွေ ထည့်ပေးပါတယ်။ Constructor လုံးဝ မရေးထားရင် Java က မမြင်ရတဲ့ default constructor တစ်ခုကို ပေးပြီး attribute တွေကို သုည တန်ဖိုးတွေ ထည့်ပေးပါတယ်: ကိန်းဂဏန်းတွေအတွက် 0၊ object တွေအတွက် null (ဘာ object ကိုမှ မညွှန်းတဲ့ တန်ဖိုး)၊ boolean (true ဒါမှမဟုတ် false တန်ဖိုးပဲ ယူတဲ့) တွေအတွက် false ပါ။
ဒါပေမယ့် constructor တစ်ခုခု ရေးလိုက်တာနဲ့ Java က အဲဒီ မမြင်ရတဲ့ constructor ကို မပေးတော့ပါဘူး။ Parameterised constructor (parameter တွေ လက်ခံတဲ့ constructor) နဲ့အတူ argument (method ကို ခေါ်တဲ့အခါ တကယ် ပေးလိုက်တဲ့ တန်ဖိုး) မပါတဲ့ ရွေးချယ်စရာကိုပါ လိုချင်ရင် နှစ်ခုလုံးကို ကိုယ်တိုင် ရေးရပါမယ်။
public class Book {
private String title;
private String author;
private int pages;
// Default constructor -- no parameters
public Book() {
this.title = "Untitled";
this.author = "Unknown";
this.pages = 0;
}
// Parameterised constructor
public Book(String title, String author, int pages) {
this.title = title;
this.author = author;
this.pages = pages;
}
public String getTitle() { return title; }
public String getAuthor() { return author; }
public int getPages() { return pages; }
}
class Book():
# Python cannot overload the constructor - a class has exactly ONE __init__.
# Default parameter values give the same effect as Java's
# default + parameterised constructor pair.
def __init__(self, title="Untitled", author="Unknown", pages=0):
self._title = title
self._author = author
self._pages = pages
def get_title(self):
return self._title
def get_author(self):
return self._author
def get_pages(self):
return self._pages
Python မှာ constructor ကို overload (နာမည်တူပြီး parameter မတူတဲ့ method များ ရေးခြင်း) လုပ်လို့ မရပါဘူး: class တစ်ခုမှာ __init__ တစ်ခုပဲ ရှိပါတယ်။ Parameter တွေကို default value ပေးထားတာက Java ရဲ့ default constructor နဲ့ parameterised constructor အတွဲလိုပဲ အကျိုး တူတူ ရပါတယ်။
Book b1 = new Book(); // uses default constructor
Book b2 = new Book("Dune", "Frank Herbert", 412); // uses parameterised constructor
System.out.println(b1.getTitle()); // Untitled
System.out.println(b2.getTitle()); // Dune
b1 = Book() # no arguments - the default values are used
b2 = Book("Dune", "Frank Herbert", 412) # arguments replace the defaults
print(b1.get_title()) # Untitled
print(b2.get_title()) # Dune
b1 က default constructor ကို သုံးပြီး b2 က parameterised constructor ကို သုံးပါတယ်။ Python မှာ argument မပေးရင် default value တွေကို သုံးပြီး argument ပေးရင် default တွေကို အစားထိုးပါတယ်။
Parameterised constructor ကို ရေးပြီး default constructor ကို ရေးဖို့ မေ့သွားရင်
new Book()ကို ခေါ်တဲ့ code က compile မဖြစ်ပါဘူး။ Constructor တစ်ခုမှ မရေးထားမှသာ Java က default constructor ကို အလိုအလျောက် ပေးပါတယ်။
Parameterised Constructor (parameter တွေ လက်ခံတဲ့ constructor)
Parameterised constructor က object ရဲ့ စတင် state ကို သတ်မှတ်ဖို့ သုံးတဲ့ argument တွေကို လက်ခံပါတယ်။ ဒါက အသုံးအများဆုံး အမျိုးအစားပါ: object တွေကို အစကတည်းက သတ်မှတ်ထားတဲ့ တန်ဖိုးတွေနဲ့ ဖန်တီးနိုင်စေပါတယ်။
public class Student {
private String name;
private int grade;
public Student(String name, int grade) {
this.name = name;
this.grade = grade;
}
public String getName() { return name; }
public int getGrade() { return grade; }
}
class Student():
def __init__(self, name, grade):
self._name = name
self._grade = grade
def get_name(self):
return self._name
def get_grade(self):
return self._grade
Student s = new Student("Maya", 11);
System.out.println(s.getName()); // Maya
System.out.println(s.getGrade()); // 11
s = Student("Maya", 11)
print(s.get_name()) # Maya
print(s.get_grade()) # 11
Overloaded Constructors (parameter list မတူတဲ့ constructor အများ)
Constructor overloading ဆိုတာ class တစ်ခုတည်းမှာ constructor အများကို ရေးပြီး တစ်ခုစီမှာ မတူတဲ့ parameter list (parameter စာရင်း) ရှိတာပါ။ ပေးလိုက်တဲ့ argument တွေကို ကြည့်ပြီး ဘယ် constructor ကို ခေါ်မလဲ ဆိုတာကို Java က ဆုံးဖြတ်ပါတယ်။
public class Rectangle {
private double width;
private double height;
// Constructor 1: both dimensions specified
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
// Constructor 2: square (equal sides)
public Rectangle(double side) {
this.width = side;
this.height = side;
}
// Constructor 3: default 1x1
public Rectangle() {
this.width = 1.0;
this.height = 1.0;
}
public double getArea() {
return width * height;
}
}
class Rectangle():
# Python cannot overload __init__ - one constructor must cover all
# three cases. Default values + a None check reproduce Java's trio.
def __init__(self, width=1.0, height=None):
if height == None:
height = width # one argument -> square; no arguments -> 1.0 x 1.0
self._width = width
self._height = height
def get_area(self):
return self._width * self._height
Java code ထဲက constructor သုံးခုက (1) အလျားနဲ့ အမြင့် နှစ်ခုလုံး ပေးတဲ့အခါ၊ (2) square (အနားတွေ တူတဲ့ စတုရန်း) အတွက်၊ (3) default 1x1 အတွက် ဖြစ်ပါတယ်။ Python မှာ __init__ ကို overload လုပ်လို့ မရလို့ constructor တစ်ခုတည်းက အခြေအနေ သုံးမျိုးလုံးကို ကိုင်တွယ်ရပါတယ်: default value တွေနဲ့ None စစ်တာက Java ရဲ့ constructor သုံးခုလို အလုပ်လုပ်ပါတယ် (argument တစ်ခုပဲ ပေးရင် စတုရန်း၊ argument မပေးရင် 1.0 x 1.0)။
Rectangle r1 = new Rectangle(5.0, 3.0); // calls constructor 1
Rectangle r2 = new Rectangle(4.0); // calls constructor 2 (square)
Rectangle r3 = new Rectangle(); // calls constructor 3 (default)
System.out.println(r1.getArea()); // 15.0
System.out.println(r2.getArea()); // 16.0
System.out.println(r3.getArea()); // 1.0
r1 = Rectangle(5.0, 3.0) # both dimensions given
r2 = Rectangle(4.0) # one argument - a square
r3 = Rectangle() # no arguments - the 1.0 x 1.0 default
print(r1.get_area()) # 15.0
print(r2.get_area()) # 16.0
print(r3.get_area()) # 1.0
r1 က constructor 1 ကို၊ r2 က constructor 2 (စတုရန်း) ကို၊ r3 က constructor 3 (default) ကို ခေါ်ပါတယ်။
Java က ပေးလိုက်တဲ့ argument တွေနဲ့ ကိုက်ညီတဲ့ parameter list ရှိတဲ့ constructor ကို ရွေးပြီး ခေါ်ပါတယ်။ ကိုက်ညီတာ မတွေ့ရင် code က compile မဖြစ်ပါဘူး။ (Python မှာ ဒီလို ရွေးတဲ့ အဆင့် မရှိပါဘူး: __init__ တစ်ခုပဲ ရှိလို့ အခြေအနေ အားလုံးကို သူကိုယ်တိုင် ကိုင်တွယ်ရပါတယ်။)
Constructor ထဲမှာ Validation လုပ်ခြင်း
မမှန်တဲ့ ဒေတာနဲ့ object တွေ ဖန်တီးမိတာကို ကာကွယ်ဖို့ constructor တွေထဲမှာ validation (တန်ဖိုး မှန်မမှန် စစ်ခြင်း) ထည့်နိုင်ပါတယ်:
public class Temperature {
private double celsius;
public Temperature(double celsius) {
if (celsius < -273.15) {
this.celsius = -273.15; // clamp to absolute zero
} else {
this.celsius = celsius;
}
}
public double getCelsius() { return celsius; }
}
class Temperature():
def __init__(self, celsius):
if celsius < -273.15:
self._celsius = -273.15 # clamp to absolute zero
else:
self._celsius = celsius
def get_celsius(self):
return self._celsius
Temperature t = new Temperature(-500.0);
System.out.println(t.getCelsius()); // -273.15 (clamped, not -500)
t = Temperature(-500.0)
print(t.get_celsius()) # -273.15 (clamped, not -500)
Comment တွေထဲက clamp (အပိုင်းအခြား ပြင်ပ ရောက်နေတဲ့ တန်ဖိုးကို အနီးဆုံး အစွန်း တန်ဖိုးနဲ့ အစားထိုးခြင်း) ဆိုတာ ဒီမှာ absolute zero (ပကတိ သုည၊ ဖြစ်နိုင်တဲ့ အနိမ့်ဆုံး အပူချိန်) ဖြစ်တဲ့ -273.15 ထက် နိမ့်တဲ့ တန်ဖိုးကို -273.15 အဖြစ် ပြောင်းထည့်တာပါ: ဒါကြောင့် -500 မဟုတ်ဘဲ -273.15 ကို print ထုတ်ပါတယ်။
ဒါက Temperature object တိုင်း မှန်ကန်တဲ့ state နဲ့ စတာကို သေချာစေပါတယ်: absolute zero ထက် နိမ့်တဲ့ အပူချိန်နဲ့ object တစ်ခုမှ မရှိနိုင်ပါဘူး။
Constructor တွေ မလုပ်သင့်တဲ့ အရာများ
Constructor တွေက attribute တွေကို စတင် တန်ဖိုး သတ်မှတ်ပြီး အခြေခံ validation လုပ်သင့်ပါတယ်။ သူတို့ မလုပ်သင့်တာတွေ က:
- ရှုပ်ထွေးတဲ့ တွက်ချက်မှုတွေ ဒါမှမဟုတ် I/O operation (file ဖတ်ခြင်း၊ ရေးခြင်းလို input၊ output လုပ်ငန်း) တွေ လုပ်ခြင်း
- Object အပြည့်အစုံ တန်ဖိုး သတ်မှတ်ပြီးမှ အလုပ်လုပ်နိုင်တဲ့ method တွေကို ခေါ်ခြင်း (object က တည်ဆောက်နေဆဲ ဖြစ်လို့ပါ)
- Output (program က ထုတ်ပြတဲ့ ရလဒ်) ကို print ထုတ်ခြင်း: constructor တွေက ဘာမှ print မထုတ်သင့်ပါဘူး၊ debugging (program ထဲက အမှားကို ရှာပြီး ပြင်ခြင်း) လုပ်နေတုန်းကလွဲရင်ပေါ့
အဆင့်ဆင့် ရှင်းပြထားတဲ့ ဥပမာများ
ဥပမာ 1: မှန်ကန်တဲ့ Constructor ကို ရွေးခြင်း
အခြေအနေ: GameCharacter class က ဂိမ်း ဇာတ်ကောင်တွေကို နည်း သုံးမျိုးနဲ့ ဖန်တီးနိုင်ရပါမယ်:
- နာမည်နဲ့ စတင် hit points (
hp၊ ဇာတ်ကောင်ရဲ့ ကျန်ရှိတဲ့ အသက် အမှတ်) နဲ့ - နာမည် တစ်ခုတည်းနဲ့ (
hpက default အဖြစ် 100 ဖြစ်ပါတယ်၊ အသက် အပြည့်) - Argument မပါဘဲ (default “Player” ဇာတ်ကောင်၊ testing (program မှန်မမှန် စမ်းစစ်ခြင်း) အတွက် အသုံးဝင်ပါတယ်)
public class GameCharacter {
private String name;
private int hp;
public GameCharacter(String name, int hp) {
this.name = name;
this.hp = hp;
}
public GameCharacter(String name) {
this.name = name;
this.hp = 100;
}
public GameCharacter() {
this.name = "Player";
this.hp = 100;
}
public String getName() { return name; }
public int getHp() { return hp; }
}
class GameCharacter():
# One __init__ covers all three Java constructors:
# defaults fill in whatever the caller leaves out
def __init__(self, name="Player", hp=100):
self._name = name
self._hp = hp
def get_name(self):
return self._name
def get_hp(self):
return self._hp
Python မှာ __init__ တစ်ခုတည်းက Java constructor သုံးခုလုံးကို ကိုယ်စားပြုပါတယ်: ခေါ်တဲ့ code က မပေးဘဲ ချန်ထားတဲ့ တန်ဖိုးတွေကို default value တွေက ဖြည့်ပေးပါတယ်။
GameCharacter c1 = new GameCharacter("Ava", 80);
GameCharacter c2 = new GameCharacter("Mira");
GameCharacter c3 = new GameCharacter();
System.out.println(c1.getName() + ": " + c1.getHp());
System.out.println(c2.getName() + ": " + c2.getHp());
System.out.println(c3.getName() + ": " + c3.getHp());
c1 = GameCharacter("Ava", 80)
c2 = GameCharacter("Mira")
c3 = GameCharacter()
print(c1.get_name() + ": " + str(c1.get_hp()))
print(c2.get_name() + ": " + str(c2.get_hp()))
print(c3.get_name() + ": " + str(c3.get_hp()))
Output:
Ava: 80
Mira: 100
Player: 100
ဥပမာ 2: Validation ပါတဲ့ Constructor
အခြေအနေ: Grade class က ရာခိုင်နှုန်း (0-100) တစ်ခုကို သိမ်းပါတယ်။ Constructor က တန်ဖိုးကို အပိုင်းအခြားထဲမှာ ထားရပါမယ်: 0 အောက် တန်ဖိုးကို 0 အဖြစ်၊ 100 ကျော်တဲ့ တန်ဖိုးကို 100 အဖြစ် သိမ်းပါတယ်။
public class Grade {
private int percentage;
public Grade(int percentage) {
if (percentage < 0) {
this.percentage = 0;
} else if (percentage > 100) {
this.percentage = 100;
} else {
this.percentage = percentage;
}
}
public int getPercentage() { return percentage; }
public String getLetterGrade() {
if (percentage >= 90) return "A";
if (percentage >= 80) return "B";
if (percentage >= 70) return "C";
if (percentage >= 60) return "D";
return "F";
}
}
class Grade():
def __init__(self, percentage):
if percentage < 0:
self._percentage = 0
elif percentage > 100:
self._percentage = 100
else:
self._percentage = percentage
def get_percentage(self):
return self._percentage
def get_letter_grade(self):
if self._percentage >= 90:
return "A"
elif self._percentage >= 80:
return "B"
elif self._percentage >= 70:
return "C"
elif self._percentage >= 60:
return "D"
else:
return "F"
Grade g1 = new Grade(95);
Grade g2 = new Grade(-10);
Grade g3 = new Grade(150);
System.out.println(g1.getPercentage() + " = " + g1.getLetterGrade());
System.out.println(g2.getPercentage() + " = " + g2.getLetterGrade());
System.out.println(g3.getPercentage() + " = " + g3.getLetterGrade());
g1 = Grade(95)
g2 = Grade(-10)
g3 = Grade(150)
print(g1.get_percentage(), "=", g1.get_letter_grade())
print(g2.get_percentage(), "=", g2.get_letter_grade())
print(g3.get_percentage(), "=", g3.get_letter_grade())
Output:
95 = A
0 = F
100 = A
Quick Check (အမြန် စစ်ဆေးမေးခွန်း)
Q1. Kettle k = new Kettle(1500); ဆိုတဲ့ line ကို run လိုက်ပါတယ်။ Constructor က ဘာလုပ်သလဲ။
public class Kettle {
private int capacityMl;
private int waterMl;
public Kettle(int capacityMl) {
this.capacityMl = capacityMl;
this.waterMl = 0;
}
} Q2. Parameterised constructor ကို ရေးပြီး default constructor ကို မရေးထားရင် ဘာဖြစ်မလဲ။
Q3. Constructor overloading ဆိုတာ ဘာလဲ။
Q4. အောက်ပါတို့ထဲက ဘယ်ဟာက constructor တွေအတွက် စည်းမျဉ်း မဟုတ် သလဲ။
Q5. ဥပမာ 1 က GameCharacter class ကို သုံးရင် new GameCharacter("Mira") က hp ကို ဘယ်တန်ဖိုး သတ်မှတ်မလဲ။
Trace Exercise (code ကို တစ်ဆင့်ချင်း လိုက်ကြည့်ခြင်း)
Rectangle object သုံးခု ဖန်တီးပုံကို Trace (တစ်ဆင့်ချင်း လိုက်ပြီး မှတ်ပါ) လုပ်ပြီး သူတို့ရဲ့ ဧရိယာတွေကို ရှာပါ။
Trace: Overloaded constructor များ
Rectangle r1 = new Rectangle(6.0, 4.0);
Rectangle r2 = new Rectangle(5.0);
Rectangle r3 = new Rectangle();| Object | width | height | getArea() |
|---|---|---|---|
| r1 | |||
| r2 | |||
| r3 |
Spot the Error (အမှား ရှာခြင်း)
ဒီ class က compile ဖြစ်ပါတယ်၊ ဒါပေမယ့် new Player("Alice", 100) က name ကို null နဲ့ score ကို 0 လို့ သတ်မှတ်ပါတယ်။ အမှားပါတဲ့ line ကို ရွေးပါ၊ ပြီးရင် ပြင်နည်းကို ရွေးပါ။
ပြင်နည်းကို ရွေးပါ:
Predict the Output (output ကို ကြိုခန့်မှန်းခြင်း)
ဥပမာ 2 က Grade class ကို သုံးပြီး:
Grade g = new Grade(-10);
System.out.println(g.getLetterGrade());ဘာကို print ထုတ်မလဲ။
ဒီ code က ဘာကို print ထုတ်မလဲ။
Grade g = new Grade(150);
System.out.println(g.getPercentage() + " " + g.getLetterGrade());Print ထုတ်မယ့် output က ဘာလဲ။
Practice Exercises (လေ့ကျင့်ခန်းများ)
Core (အခြေခံ အဆင့်)
-
Constructor တစ်ခု ရေးပါ:
title(String)၊artist(String) နဲ့durationSeconds(int) attribute တွေပါတဲ့Songclass တစ်ခု ဖန်တီးပါ။ Parameterised constructor တစ်ခုနဲ့ attribute သုံးခုလုံးအတွက် getter (private attribute ရဲ့ တန်ဖိုးကို ပြန်ပေးတဲ့ method) တွေကို ရေးပါ။main(program စ run တဲ့ method) ထဲမှာ Song object နှစ်ခု ဖန်တီးပြီး သူတို့ရဲ့ အသေးစိတ်ကို print ထုတ်ပါ။ -
Default + parameterised: title ကို “Unknown”၊ artist ကို “Unknown” နဲ့ duration ကို 0 လို့ သတ်မှတ်ပေးတဲ့ default constructor ကို ကိုယ့်
Songclass ထဲ ထည့်ပါ။ Constructor နှစ်ခုလုံးကို စမ်းသပ်ပါ။ -
ဘယ် constructor လဲ:
Car()၊Car(String make)နဲ့Car(String make, int year)constructor တွေပါတဲ့ class တစ်ခု ရှိပါတယ်။ အောက်ပါ တစ်ခုစီအတွက် ဘယ် constructor ကို ခေါ်လဲ State (ဖော်ပြပါ) လုပ်ပါ:new Car()new Car("Toyota", 2024)new Car("Honda")
Extension (တိုးချဲ့ အဆင့်)
-
Validation ပါတဲ့ constructor:
hours(0-23) နဲ့minutes(0-59) ပါတဲ့Clockclass တစ်ခု ဖန်တီးပါ။ Constructor က အပိုင်းအခြား ပြင်ပ ရောက်နေတဲ့ တန်ဖိုးတွေကို clamp လုပ်ရပါမယ်။ အချိန်ကို “HH:MM” ပုံစံနဲ့ return လုပ်ပေးတဲ့getTime()method ကို ထည့်ပါ။ မှန်တဲ့ input (program ထဲ ထည့်ပေးတဲ့ ဒေတာ) တွေနဲ့ မမှန်တဲ့ input တွေနဲ့ စမ်းသပ်ပါ။ -
Overloaded constructor များ:
radiusattribute ပါတဲ့Circleclass တစ်ခု ဖန်တီးပါ။ Constructor သုံးခု ရေးပါ: radius (အချင်းဝက်) ကိုdoubleအဖြစ် ယူတဲ့ constructor တစ်ခု၊ diameter (အချင်း) ကို ကိန်းပြည့် (int) အဖြစ် ယူပြီး တစ်ဝက်ခွဲကာ radius ကို သတ်မှတ်တဲ့ constructor တစ်ခု၊ unit circle (radius 1 ရှိတဲ့ စက်ဝိုင်း) ကို ဖန်တီးတဲ့ default constructor တစ်ခု။ (doubleတစ်ခုစီ ယူတဲ့ constructor နှစ်ခု အတူ မရှိနိုင်ပါဘူး: overload လုပ်ထားတဲ့ constructor တွေကို Java က parameter list နဲ့ ခွဲခြားလို့ပါ။)getArea()method တစ်ခု ထည့်ပါ။
Challenge (စိန်ခေါ်မှု အဆင့်)
- အခြေအနေကနေ ဒီဇိုင်းဆွဲခြင်း: ရုပ်ရှင်ရုံ လက်မှတ် ကြိုတင်မှာတဲ့ စနစ် တစ်ခုမှာ
Bookingclass လိုပါတယ်။ Booking တစ်ခုကို ဒီနည်းတွေနဲ့ ဖန်တီးနိုင်ရပါမယ်: (a) customer နာမည်၊ ရုပ်ရှင် ခေါင်းစဉ်နဲ့ ထိုင်ခုံ အရေအတွက်၊ (b) customer နာမည်နဲ့ ရုပ်ရှင် ခေါင်းစဉ်ပဲ (ထိုင်ခုံ 1 ခုက default)၊ (c) argument မပါဘဲ (ကြိုမမှာဘဲ ရုံကို တိုက်ရိုက် လာတဲ့ walk-in booking၊ နာမည်က “Guest”)။ Constructor သုံးခုလုံး၊ validation (ထိုင်ခုံ 1-10 ခု ဖြစ်ရမယ်) နဲ့ ထိုင်ခုံ တစ်ခုကို $12.50 ကျတယ်လို့ ယူဆပြီးgetTotalPrice()method တို့ပါတဲ့ class ကို Design (အစီအစဉ် ဒါမှမဟုတ် model တစ်ခု ရေးဆွဲပါ) လုပ်ပြီး ရေးပါ။
ဆက်စပ် ခေါင်းစဉ်များ
- အရင် လေ့လာထားရမယ့် ခေါင်းစဉ်: Classes and Objects (class နဲ့ object များ): object တွေကို ဘယ်လို စတင် တန်ဖိုး သတ်မှတ်မလဲ မလေ့လာခင် class နဲ့ object ဆိုတာ ဘာလဲ နားလည်ထားဖို့
- နောက်ထပ် လေ့လာမယ့် ခေါင်းစဉ်: The
thisKeyword: constructor တွေက parameter တွေနဲ့ instance variable တွေကို ဘယ်လို ခွဲခြားလဲ - ဆက်စပ် ခေါင်းစဉ်: Encapsulation (ဒေတာနဲ့ method ကို တစ်စုတည်း ထုပ်ပိုးခြင်း): constructor တွေက private attribute တွေ၊ validation တွေနဲ့ အတူ အလုပ်လုပ်ပါတယ်
- နောက်ပိုင်းမှာ: Inheritance (parent class ကနေ အမွေဆက်ခံခြင်း): child class ရဲ့ constructor တွေက
super()နဲ့ parent ရဲ့ constructor တွေကို ခေါ်ပါတယ်