fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

[TypeScript] 第1551天 在TypeScript中如何实现继承?

Open haizhilin2013 opened this issue 2 years ago • 1 comments

第1551天 在TypeScript中如何实现继承?

3+1官网

我也要出题

haizhilin2013 avatar Jul 14 '23 20:07 haizhilin2013

在 TypeScript 中,实现继承的主要方式是使用类(class)和接口(interface)。通过 extends 关键字,类可以继承另一个类的属性和方法;通过 implements 关键字,类可以实现一个或多个接口的规范。以下是详细的实现方法和示例:

1. 类继承(Class Inheritance)

基本类继承

使用 extends 关键字来实现类的继承。子类继承父类的所有属性和方法,可以重写(override)父类的方法,也可以添加新的属性和方法。

class Animal {
    name: string;

    constructor(name: string) {
        this.name = name;
    }

    move(distance: number = 0) {
        console.log(`${this.name} moved ${distance} meters.`);
    }
}

class Dog extends Animal {
    constructor(name: string) {
        super(name); // 调用父类的构造函数
    }

    bark() {
        console.log('Woof! Woof!');
    }
}

const dog = new Dog('Rex');
dog.bark(); // 输出: Woof! Woof!
dog.move(10); // 输出: Rex moved 10 meters.

在这个示例中,Dog 类继承了 Animal 类,Dog 类可以调用父类的 move 方法,并且定义了自己的 bark 方法。

重写方法

子类可以重写父类的方法,以提供特定的实现。

class Bird extends Animal {
    constructor(name: string) {
        super(name);
    }

    move(distance: number = 10) {
        console.log('Flying...');
        super.move(distance);
    }
}

const bird = new Bird('Sparrow');
bird.move(); // 输出: Flying... Sparrow moved 10 meters.

在这个示例中,Bird 类重写了 Animal 类的 move 方法,并在调用父类的 move 方法之前添加了自己的实现。

2. 接口实现(Interface Implementation)

类可以通过 implements 关键字来实现一个或多个接口。接口定义了类应该具有的属性和方法,但不提供具体实现。

interface Flyable {
    fly(): void;
}

class Helicopter implements Flyable {
    fly() {
        console.log('Helicopter is flying.');
    }
}

class Airplane implements Flyable {
    fly() {
        console.log('Airplane is flying.');
    }
}

const helicopter = new Helicopter();
helicopter.fly(); // 输出: Helicopter is flying.

const airplane = new Airplane();
airplane.fly(); // 输出: Airplane is flying.

在这个示例中,HelicopterAirplane 类实现了 Flyable 接口,必须提供 fly 方法的具体实现。

3. 多重继承接口(Multiple Interface Inheritance)

TypeScript 支持一个类实现多个接口。

interface Swimmable {
    swim(): void;
}

class Duck implements Flyable, Swimmable {
    fly() {
        console.log('Duck is flying.');
    }

    swim() {
        console.log('Duck is swimming.');
    }
}

const duck = new Duck();
duck.fly(); // 输出: Duck is flying.
duck.swim(); // 输出: Duck is swimming.

在这个示例中,Duck 类同时实现了 FlyableSwimmable 接口,必须提供这两个接口中定义的所有方法。

4. 抽象类(Abstract Class)

抽象类是不能直接实例化的类,可以包含具体实现和抽象方法(没有具体实现的方法)。子类必须实现所有抽象方法。

abstract class Animal {
    constructor(public name: string) {}

    abstract makeSound(): void; // 抽象方法,没有具体实现

    move(): void {
        console.log(`${this.name} is moving.`);
    }
}

class Lion extends Animal {
    makeSound() {
        console.log('Roar!');
    }
}

const lion = new Lion('Simba');
lion.makeSound(); // 输出: Roar!
lion.move(); // 输出: Simba is moving.

在这个示例中,Animal 是一个抽象类,包含一个抽象方法 makeSound 和一个具体方法 moveLion 类继承 Animal 并实现了 makeSound 方法。

通过这些方式,TypeScript 提供了丰富的面向对象编程支持,使得继承、接口实现等概念可以灵活应用于代码设计中。

llccing avatar Jun 26 '24 07:06 llccing