TypeScript Klassenvererbung und Polymorphismus
TypeScript unterstützt die Vererbungs- und Polymorphismus-Funktionen der objektorientierten Programmierung.
SVG-Diagramm: KlassenvererbungshierarchieKlassenvererbung
Verwenden Sie das Schlüsselwort extends, um Vererbung zu implementieren.
Beispiel
class Animal {
name: string;
constructor(name: string) {
this.name = name;
}
speak(): void {
console.log(this.name + " make a sound");
}
}
class Dog extends Animal {
breed: string;
constructor(name: string, breed: string) {
super(name); // Konstruktor der Elternklasse aufrufen
this.breed = breed;
}
speak(): void {
console.log(this.name + " Wau wau wau!");
}
}
var dog = new Dog("Wangcai", "Golden Retriever");
dog.speak();
name: string;
constructor(name: string) {
this.name = name;
}
speak(): void {
console.log(this.name + " make a sound");
}
}
class Dog extends Animal {
breed: string;
constructor(name: string, breed: string) {
super(name); // Konstruktor der Elternklasse aufrufen
this.breed = breed;
}
speak(): void {
console.log(this.name + " Wau wau wau!");
}
}
var dog = new Dog("Wangcai", "Golden Retriever");
dog.speak();
Ausgabe:
Wangcai woof woof woof!
super-Schlüsselwort
super wird verwendet, um die Methoden und den Konstruktor der Elternklasse aufzurufen.
Beispiel
class Shape {
color: string;
constructor(color: string) {
this.color = color;
}
describe(): string {
return "Dies ist eine " + this.color + " Form";
}
}
class Circle extends Shape {
radius: number;
constructor(color: string, radius: number) {
super(color);
this.radius = radius;
}
// Methode der Elternklasse überschreiben
describe(): string {
// Methode der Elternklasse aufrufen und erweitern
return super.describe() + ", Radius ist " + this.radius;
}
area(): number {
return Math.PI * this.radius * this.radius;
}
}
var circle = new Circle("red", 5);
console.log(circle.describe());
console.log("Fläche: " + circle.area().toFixed(2));
color: string;
constructor(color: string) {
this.color = color;
}
describe(): string {
return "Dies ist eine " + this.color + " Form";
}
}
class Circle extends Shape {
radius: number;
constructor(color: string, radius: number) {
super(color);
this.radius = radius;
}
// Methode der Elternklasse überschreiben
describe(): string {
// Methode der Elternklasse aufrufen und erweitern
return super.describe() + ", Radius ist " + this.radius;
}
area(): number {
return Math.PI * this.radius * this.radius;
}
}
var circle = new Circle("red", 5);
console.log(circle.describe());
console.log("Fläche: " + circle.area().toFixed(2));
Ausgabe:
这是一个 红色的图形,半径是 5 面积: 78.54
Polymorphismus
Eine Instanz einer Unterklasse kann einem Typ der Elternklasse zugewiesen werden.
Beispiel
class Animal {
name: string;
constructor(name: string) { this.name = name; }
speak(): void {
console.log(this.name + " make a sound");
}
}
class Cat extends Animal {
speak(): void {
console.log(this.name + " Miau miau miau!");
}
}
class Dog extends Animal {
speak(): void {
console.log(this.name + " Wau wau wau!");
}
}
// Polymorphismus: Instanzen verschiedener Unterklassen in einem Array speichern
var animals: Animal[] = [
new Cat("Xiaobai"),
new Dog("Wangcai"),
new Animal("Tiere")
];
// Dieselbe Methode aufrufen; verschiedene Unterklassen haben unterschiedliche Implementierungen
for (var _i = 0, animals_1 = animals; _i < animals_1.length; _i++) {
var animal = animals_1[_i];
animal.speak();
}
name: string;
constructor(name: string) { this.name = name; }
speak(): void {
console.log(this.name + " make a sound");
}
}
class Cat extends Animal {
speak(): void {
console.log(this.name + " Miau miau miau!");
}
}
class Dog extends Animal {
speak(): void {
console.log(this.name + " Wau wau wau!");
}
}
// Polymorphismus: Instanzen verschiedener Unterklassen in einem Array speichern
var animals: Animal[] = [
new Cat("Xiaobai"),
new Dog("Wangcai"),
new Animal("Tiere")
];
// Dieselbe Methode aufrufen; verschiedene Unterklassen haben unterschiedliche Implementierungen
for (var _i = 0, animals_1 = animals; _i < animals_1.length; _i++) {
var animal = animals_1[_i];
animal.speak();
}
Ausgabe:
Xiaobai meow meow meow! Wangcai woof woof woof! Animals make sounds.
instanceof-Prüfung
Verwende instanceof, um den Instanztyp zu prüfen.
Beispiel
class Rectangle {
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
area(): number {
return this.width * this.height;
}
}
class Circle {
radius: number;
constructor(radius: number) {
this.radius = radius;
}
area(): number {
return Math.PI * this.radius ** 2;
}
}
var shapes = [new Rectangle(4, 5), new Circle(3)];
for (var _i = 0, shapes_1 = shapes; _i < shapes_1.length; _i++) {
var shape = shapes_1[_i];
if (shape instanceof Rectangle) {
console.log("Rechteckfläche: " + shape.area());
} else if (shape instanceof Circle) {
console.log("Kreisfläche: " + shape.area().toFixed(2));
}
}
width: number;
height: number;
constructor(width: number, height: number) {
this.width = width;
this.height = height;
}
area(): number {
return this.width * this.height;
}
}
class Circle {
radius: number;
constructor(radius: number) {
this.radius = radius;
}
area(): number {
return Math.PI * this.radius ** 2;
}
}
var shapes = [new Rectangle(4, 5), new Circle(3)];
for (var _i = 0, shapes_1 = shapes; _i < shapes_1.length; _i++) {
var shape = shapes_1[_i];
if (shape instanceof Rectangle) {
console.log("Rechteckfläche: " + shape.area());
} else if (shape instanceof Circle) {
console.log("Kreisfläche: " + shape.area().toFixed(2));
}
}
Ausgabe:
矩形面积: 20 圆形面积: 28.27
protected Member
Geschützte Member sind in Unterklassen sichtbar.
Beispiel
class Person {
protected name: string;
constructor(name: string) {
this.name = name;
}
}
class Employee extends Person {
private department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
public introduce(): string {
// Kann auf protected-Member zugreifen
return "Ich bin " + this.name + ", in " + this.department + " working";
}
}
var emp = new Employee("Alice", "Technology Department");
console.log(emp.introduce());
// console.log(emp.name); // Error: protected ist von außerhalb nicht zugänglich
protected name: string;
constructor(name: string) {
this.name = name;
}
}
class Employee extends Person {
private department: string;
constructor(name: string, department: string) {
super(name);
this.department = department;
}
public introduce(): string {
// Kann auf protected-Member zugreifen
return "Ich bin " + this.name + ", in " + this.department + " working";
}
}
var emp = new Employee("Alice", "Technology Department");
console.log(emp.introduce());
// console.log(emp.name); // Error: protected ist von außerhalb nicht zugänglich
Ausgabe:
我是 Alice,在 技术部 工作
Zusammenfassung
- Vererbung:extends-Schlüsselwort
- super:Aufruf der Elternklasse
- Polymorphismus:Gleiches Interface, unterschiedliche Implementierungen
- protected:In Unterklassen sichtbar