JavaScript တွင် OOPS အမွေဆက်ခံခြင်းအတွက် Constructor

အမွေဆက်ခံရာတွင် မိဘ class ၏ constructor ကို ပြန်လည်သတ်မှတ်နိုင်ပါသည်။ ဥပမာတစ်ခုဖြင့် ကြည့်ရအောင်။ ကျွန်ုပ်တို့တွင် အောက်ပါမိဘ class ရှိသည်ဆိုပါစို့။

javascriptclass User {
  constructor(name, surn) {
    this.name = name;
    this.surn = surn;
  }
  
  getName() {
    return this.name;
  }
  getSurn() {
    return this.surn;
  }
}

မိဘ class မှ အောက်ပါ class က အမွေဆက်ခံသည်ဆိုပါစို့။

javascriptclass Student extends User {
  
}

ဆက်ခံသော class တွင် ထပ်ဆောင်း parameter များကို ထည့်သွင်း၍ constructor ကို ချဲ့ထွင်လိုသည်ဆိုပါစို့။

javascriptclass Student extends User {
  constructor(name, surn, year) {
    
  }
}

ထိုသို့ဆိုလျှင် ပထမဆုံးစာကြောင်းအဖြစ် super ဖြင့် မိဘ class ၏ constructor ကို ခေါ်ရပါမည်။

javascriptclass Student extends User {
  constructor(name, surn, year) {
    super();
  }
}

အမိန့် super သည် အခြေခံအားဖြင့် မိဘ class ၏ constructor ဖြစ်သည်။ ထို့ကြောင့် ၎င်းထဲသို့ လိုအပ်သော parameter များကို ပေးပို့ပါမည်။

javascriptclass Student extends User {
  constructor(name, surn, year) {
    super(name, surn);
  }
}

ယခု ဆက်ခံသော class တွင် ပညာသင်နှစ်ကို ၎င်း၏ကိုယ်ပိုင် property တွင် ရေးသားပါမည်။

javascriptclass Student extends User {
  constructor(name, surn, year) {
    super(name, surn);
    this.year = year;
  }
}

ပညာသင်နှစ်အတွက် getter method ကို ပြုလုပ်ပါမည်။

javascriptclass Student extends User {
  constructor(name, surn, year) {
    super(name, surn);
    this.year = year;
  }
  
  getYear() {
    return this.year;
  }
}
Задание 1

Employee class သည် ဤသင်ခန်းစာရှိ User class မှ အမွေဆက်ခံသည်ဆိုပါစို့။

Задание 2

Employee class တွင် မိဘ class ၏ constructor ကို အသက်နှင့် လစာတို့အတွက် parameter များထည့်သွင်း၍ ပြန်လည်သတ်မှတ်ပါ။

Задание 3

အသက်နှင့် လစာအတွက် getter method များ ပြုလုပ်ပါ။