Rajapintojen laajentaminen OOP:ssa TypeScriptissä

Rajapinnat TypeScriptissä voivat periä toisiltaan. Tällaista vuorovaikutusta kutsutaan rajapintojen laajentamiseksi. Katsotaanpa esimerkkiä. Oletetaan, että meillä on rajapinta ISize:

typescriptinterface ISize {
  height: number;
  width: number;
}

Tehdään rajapinta IStyle, joka laajentaa ISize:

typescriptinterface IStyle extends ISize {
  color: string;
}

Nyt tehdään luokka Figure, joka toteuttaa rajapinnan IStyle. Meidän luokkamme on toteutettava sekä rajapinnan ominaisuudet että sen vanhemman. Tehdään tämä:

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;
  }
}

Tarkistetaan toiminta:

typescriptlet fig = new Figure(130, 200, 'vihreä');
console.log(fig);
Задание 1

Luo rajapinta IProgrammer, jossa on ominaisuudet salary ja language. Antaa tämän rajapinnan periytyä IUser:sta, jossa on ominaisuudet name, birthday.

Задание 2

Tee luokka Employee, joka toteuttaa IProgrammer:n.