JavaScript အတန်းအစား အမွေအနှစ်
Class Inheritance
အတန်းအမွေဆက်ခံခြင်းကို ဖန်တီးရန် extends
သော့ချက်စကားလုံးကို အသုံးပြုပါ။
class inheritance ဖြင့် ဖန်တီးထားသော class သည် အခြား class မှ method အားလုံးကို အမွေဆက်ခံသည်-
ဥပမာ
"ကား" အတန်းမှ နည်းလမ်းများကို အမွေဆက်ခံမည့် "Model" ဟု အမည်ပေးထားသော အတန်းကို ဖန်တီးပါ-
class Car {
constructor(brand) {
this.carname =
brand;
}
present() {
return 'I have a ' + this.carname;
}
}
class Model extends Car {
constructor(brand, mod) {
super(brand);
this.model = mod;
}
show() {
return this.present() + ', it is a ' + this.model;
}
}
let myCar = new Model("Ford", "Mustang");
document.getElementById("demo").innerHTML
= myCar.show();
super()
method သည် parent class ကိုရည်ညွှန်းသည် ။
constructor method တွင် method ကို ခေါ်ခြင်းဖြင့် super()
၊ parent ၏ constructor method ကိုခေါ်ပြီး parent ၏ properties နှင့် method များသို့ ဝင်ရောက်ခွင့် ရရှိပါသည်။
Inheritance သည် ကုဒ်ပြန်သုံးနိုင်မှုအတွက် အသုံးဝင်သည်- သင်အတန်းသစ်တစ်ခုဖန်တီးသောအခါ ရှိပြီးသားအတန်း၏ ဂုဏ်သတ္တိများနှင့် နည်းလမ်းများကို ပြန်လည်အသုံးပြုပါသည်။
Getters နှင့် Setters များ
အတန်းများသည် getters နှင့် setters များကို အသုံးပြုနိုင်သည်။
အထူးသဖြင့် သင့်ဂုဏ်သတ္တိများအတွက် getters နှင့် setters များကို အသုံးပြုခြင်းသည် အထူးသဖြင့် ၎င်းတို့ကို မပြန်မီ သို့မဟုတ် သင်သတ်မှတ်ခြင်းမပြုမီ တန်ဖိုးနှင့် အထူးတစ်ခုခုပြုလုပ်လိုပါက စမတ်ကျပါသည်။
အတန်းထဲတွင် getters နှင့် setter များထည့်ရန်၊
get
နှင့် set
keywords ကိုသုံးပါ။
ဥပမာ
"carname" ပိုင်ဆိုင်မှုအတွက် getter နှင့် setter တစ်ခုကို ဖန်တီးပါ-
class Car {
constructor(brand) {
this.carname
= brand;
}
get cnam() {
return this.carname;
}
set cnam(x) {
this.carname = x;
}
}
let myCar = new Car("Ford");
document.getElementById("demo").innerHTML = myCar.cnam;
မှတ်ချက်- getter သည် method တစ်ခုဖြစ်လျှင်ပင်၊ သင်သည် ပစ္စည်းတန်ဖိုးကို ရယူလိုသောအခါတွင် ကွင်းကွင်းကို အသုံးမပြုပါ။
ဤအခြေအနေတွင် getter/setter method ၏အမည်သည် ပိုင်ဆိုင်မှုအမည်နှင့် တူညီနိုင်မည်မဟုတ်ပေ carname
။
ပရိုဂရမ်မာများစွာ _
သည် အမှန်တကယ်ပိုင်ဆိုင်မှုနှင့် getter/setter ကို ခွဲခြားရန် ပိုင်ဆိုင်မှုအမည်ရှေ့တွင် အောက်တန်းအက္ခရာကို အသုံးပြုသည်-
ဥပမာ
getter/setter ကို အမှန်တကယ်ပိုင်ဆိုင်မှုနှင့် ခွဲခြားရန် အောက်တန်းအက္ခရာကို သင်သုံးနိုင်သည်။
class Car {
constructor(brand) {
this._carname
= brand;
}
get carname() {
return this._carname;
}
set carname(x) {
this._carname = x;
}
}
let myCar = new Car("Ford");
document.getElementById("demo").innerHTML = myCar.carname;
setter ကို အသုံးပြုရန် ၊ ကွင်းစဥ်မပါဘဲ ပိုင်ဆိုင်မှုတန်ဖိုးကို သတ်မှတ်သည့်အခါ တူညီသော syntax ကိုအသုံးပြုပါ-
ဥပမာ
ကားအမည်ကို "Volvo" သို့ပြောင်းရန် setter ကိုသုံးပါ-
class Car {
constructor(brand) {
this._carname
= brand;
}
get carname() {
return this._carname;
}
set carname(x) {
this._carname = x;
}
}
let myCar = new Car("Ford");
myCar.carname = "Volvo";
document.getElementById("demo").innerHTML = myCar.carname;
လွှင့်တင်ခြင်း။
လုပ်ဆောင်ချက်များနှင့် အခြားသော JavaScript ကြေငြာချက်များနှင့်မတူဘဲ၊ အတန်းခွဲကြေငြာချက်များကို လွှင့်တင်မည်မဟုတ်ပါ။
ဆိုလိုသည်မှာ သင် ၎င်းကို အသုံးမပြုမီ အတန်းတစ်ခုကို ကြေညာရမည်ဟု ဆိုလိုသည်။
ဥပမာ
//You cannot use the class yet.
//myCar = new Car("Ford")
//This would
raise an error.
class Car {
constructor(brand) {
this.carname = brand;
}
}
//Now you can use the class:
let myCar = new Car("Ford")
မှတ်ချက်- လုပ်ဆောင်ချက်များကဲ့သို့ အခြားသော ကြေငြာချက်များအတွက်၊ ၎င်းကို မကြေငြာမီတွင် သင်အသုံးပြုရန် ကြိုးစားသောအခါတွင် အမှားအယွင်းမရှိနိုင်ပါ။ အကြောင်းမှာ JavaScript ကြေငြာချက်များ၏ မူရင်းအပြုအမူသည် လွှင့်တင်နေပါသည် (ကြေငြာချက်ကို အပေါ်မှရွှေ့ခြင်း)။