Rozširovanie rozhraní v OOP v TypeScript
Rozhrania v TypeScript môžu dediť
jeden od druhého. Takáto interakcia
sa nazýva rozširovanie rozhraní.
Pozrime sa na príklad. Nech máme
rozhranie ISize:
interface ISize {
height: number;
width: number;
}
Vytvorme rozhranie IStyle, ktoré
rozširuje ISize:
interface IStyle extends ISize {
color: string;
}
Teraz vytvorme triedu Figure,
ktorá implementuje rozhranie IStyle. Naša
trieda bude musieť implementovať ako vlastnosť
samotného rozhrania, tak aj jeho rodiča.
Urobme 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;
}
}
Skontrolujme fungovanie:
let fig = new Figure(130, 200, 'green');
console.log(fig);
Vytvorte rozhranie IProgrammer s
vlastnosťami salary a language.
Nech toto rozhranie dedí od IUser
s vlastnosťami name, birthday.
Vytvorte triedu Employee, ktorá
implementuje IProgrammer.