Interfeisu paplašināšana OOP TypeScript
Interfeisi TypeScript var mantot
viens no otra. Šādu mijiedarbību
sauk par interfeisu paplašināšanu.
Apskatīsim piemērā. Pieņemsim, ka mums
ir interfeiss ISize:
typescriptinterface ISize {
height: number;
width: number;
}
Izveidosim interfeisu IStyle, kurš
paplašinās ISize:
typescriptinterface IStyle extends ISize {
color: string;
}
Tagad izveidosim klasi Figure,
kas realizē interfeisu IStyle. Mūsu
klasei būs jārealizē gan paša interfeisa īpašība,
gan tā vecāka īpašības.
Izveidosim to:
typescriptclass 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;
}
}
Pārbaudīsim darbību:
typescriptlet fig = new Figure(130, 200, 'green');
console.log(fig);
Задание 1
Izveidojiet interfeisu IProgrammer ar
īpašībām salary un language.
Lai šis interfeiss manto IUser
ar īpašībām name, birthday.
Задание 2
Izveidojiet klasi Employee, kas
realizē IProgrammer.