TypeScript Class Inheritance and Polymorphism
TypeScript supports the inheritance and polymorphism features of object-oriented programming.
SVG Diagram: Class Inheritance HierarchyClass Inheritance
Use the extends keyword to implement inheritance.
Example
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); // Call the parent class constructor
this.breed = breed;
}
speak(): void {
console.log(this.name + " Woof woof woof!");
}
}
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); // Call the parent class constructor
this.breed = breed;
}
speak(): void {
console.log(this.name + " Woof woof woof!");
}
}
var dog = new Dog("Wangcai", "Golden Retriever");
dog.speak();
Output:
Wangcai woof woof woof!
super keyword
super is used to call the methods and constructor of the parent class.
Example
class Shape {
color: string;
constructor(color: string) {
this.color = color;
}
describe(): string {
return "This is a " + this.color + " shape";
}
}
class Circle extends Shape {
radius: number;
constructor(color: string, radius: number) {
super(color);
this.radius = radius;
}
// Override parent class method
describe(): string {
// Call the parent class method and extend it
return super.describe() + ", radius is " + this.radius;
}
area(): number {
return Math.PI * this.radius * this.radius;
}
}
var circle = new Circle("red", 5);
console.log(circle.describe());
console.log("Area: " + circle.area().toFixed(2));
color: string;
constructor(color: string) {
this.color = color;
}
describe(): string {
return "This is a " + this.color + " shape";
}
}
class Circle extends Shape {
radius: number;
constructor(color: string, radius: number) {
super(color);
this.radius = radius;
}
// Override parent class method
describe(): string {
// Call the parent class method and extend it
return super.describe() + ", radius is " + this.radius;
}
area(): number {
return Math.PI * this.radius * this.radius;
}
}
var circle = new Circle("red", 5);
console.log(circle.describe());
console.log("Area: " + circle.area().toFixed(2));
Output:
这是一个 红色的图形,半径是 5 面积: 78.54
Polymorphism
An instance of a subclass can be assigned to a parent class type.
Example
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 + " Meow meow meow!");
}
}
class Dog extends Animal {
speak(): void {
console.log(this.name + " Woof woof woof!");
}
}
// Polymorphism: store instances of different subclasses in an array
var animals: Animal[] = [
new Cat("Xiaobai"),
new Dog("Wangcai"),
new Animal("Animals")
];
// Call the same method; different subclasses have different implementations
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 + " Meow meow meow!");
}
}
class Dog extends Animal {
speak(): void {
console.log(this.name + " Woof woof woof!");
}
}
// Polymorphism: store instances of different subclasses in an array
var animals: Animal[] = [
new Cat("Xiaobai"),
new Dog("Wangcai"),
new Animal("Animals")
];
// Call the same method; different subclasses have different implementations
for (var _i = 0, animals_1 = animals; _i < animals_1.length; _i++) {
var animal = animals_1[_i];
animal.speak();
}
Output:
Xiaobai meow meow meow! Wangcai woof woof woof! Animals make sounds.
instanceof check
Use instanceof to check the instance type.
Example
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("Rectangle area: " + shape.area());
} else if (shape instanceof Circle) {
console.log("Circle area: " + 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("Rectangle area: " + shape.area());
} else if (shape instanceof Circle) {
console.log("Circle area: " + shape.area().toFixed(2));
}
}
Output:
矩形面积: 20 圆形面积: 28.27
protected members
Protected members are visible in subclasses.
Example
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 {
// Can access protected members
return "I am " + this.name + ", in " + this.department + " working";
}
}
var emp = new Employee("Alice", "Technology Department");
console.log(emp.introduce());
// console.log(emp.name); // Error: protected is not accessible from outside
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 {
// Can access protected members
return "I am " + this.name + ", in " + this.department + " working";
}
}
var emp = new Employee("Alice", "Technology Department");
console.log(emp.introduce());
// console.log(emp.name); // Error: protected is not accessible from outside
Output:
我是 Alice,在 技术部 工作
Summary
- Inheritance:extends keyword
- super:Call the parent class
- Polymorphism:Same interface, different implementations
- protected:Visible in subclasses