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:
interface ISize {
height: number;
width: number;
}
Izveidosim interfeisu IStyle, kurš
paplašinās ISize:
interface 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:
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;
}
}
Pārbaudīsim darbību:
let fig = new Figure(130, 200, 'green');
console.log(fig);
Izveidojiet interfeisu IProgrammer ar
īpašībām salary un language.
Lai šis interfeiss manto IUser
ar īpašībām name, birthday.
Izveidojiet klasi Employee, kas
realizē IProgrammer.