TypeScript တွင် OOP အထဲတွင် interface များကို ချဲ့ထွင်ခြင်း
TypeScript တွင် interface များသည် တစ်ခုနှင့်တစ်ခု အမွေဆက်ခံနိုင်ပါသည်။ ထိုသို့သော အပြန်အလှန်ဆက်သွယ်မှုကို interface များ၏ ချဲ့ထွင်ခြင်း ဟုခေါ်ပါသည်။
ဥပမာတစ်ခုကို ကြည့်ကြပါစို့။ ကျွန်ုပ်တို့တွင် ISize interface တစ်ခုရှိသည်ဆိုပါစို့။
interface ISize {
height: number;
width: number;
}
ISize ကို ချဲ့ထွင်မည့် IStyle interface တစ်ခုကို ဖန်တီးကြပါစို့။
interface IStyle extends ISize {
color: string;
}
ယခု IStyle interface ကို အကောင်အထည်ဖော်မည့် Figure class တစ်ခုကို ဖန်တီးကြပါစို့။ ကျွန်ုပ်တို့၏ class သည် ၎င်း၏ မိဘ interface ၏ ဂုဏ်သတ္တိများကိုသာမက ၎င်း၏ ကိုယ်ပိုင် ဂုဏ်သတ္တိများကိုပါ အကောင်အထည်ဖော်ရပါမည်။
ဖန်တီးကြည့်ကြပါစို့။
class Figure implements IStyle {
height: number;
width: number;
color: string;
constructor(height: number, width: number, color: string) {
this.height = height;
this.width = width;
this.color = color;
}
}
အလုပ်လုပ်ပုံကို စစ်ဆေးကြည့်ကြပါစို့။
let fig = new Figure(130, 200, 'green');
console.log(fig);
salary နှင့် language ဂုဏ်သတ္တိများပါရှိသော IProgrammer interface တစ်ခုကို ဖန်တီးပါ။
ထို interface သည် name, birthday ဂုဏ်သတ္တိများပါရှိသော IUser interface မှ အမွေဆက်ခံပါစေ။
IProgrammer ကို အကောင်အထည်ဖော်မည့် Employee class တစ်ခုကို ဖန်တီးပါ။