TS 中类型的继承

在 TypeScript(TS)中,类型的继承通常通过接口(Interfaces)和类(Classes)来实现。接口提供了一种定义对象形状的方式,而类则提供了一种创建对象实例的方式。以下是如何在 TypeScript 中实现类型继承的详细说明。

1. 使用接口继承接口

接口可以继承其他接口,从而组合和扩展多个接口的功能。

typescript 复制代码
interface Animal {
    name: string;
    eat(): void;
}

interface Dog extends Animal {
    breed: string;
    bark(): void;
}

const myDog: Dog = {
    name: "Buddy",
    breed: "Golden Retriever",
    eat() {
        console.log(`${this.name} is eating.`);
    },
    bark() {
        console.log(`${this.name} is barking.`);
    }
};

在这个例子中,Dog 接口继承了 Animal 接口,因此 Dog 必须实现 Animal 接口中的所有属性和方法,同时还可以添加新的属性和方法。

2. 使用类实现接口

类可以实现一个或多个接口,确保类包含接口中定义的属性和方法。

typescript 复制代码
interface Animal {
    name: string;
    eat(): void;
}

class Dog implements Animal {
    name: string;

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

    eat() {
        console.log(`${this.name} is eating.`);
    }

    bark() {
        console.log(`${this.name} is barking.`);
    }
}

const myDog = new Dog("Buddy");
myDog.eat();
myDog.bark();

在这个例子中,Dog 类实现了 Animal 接口,因此它必须包含 name 属性和 eat 方法。Dog 类还可以添加额外的属性和方法,如 bark

3. 类继承类

类可以继承其他类,从而复用和扩展父类的属性和方法。

typescript 复制代码
class Animal {
    name: string;

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

    eat() {
        console.log(`${this.name} is eating.`);
    }
}

class Dog extends Animal {
    breed: string;

    constructor(name: string, breed: string) {
        super(name); // 调用父类的构造函数
        this.breed = breed;
    }

    bark() {
        console.log(`${this.name} is barking.`);
    }
}

const myDog = new Dog("Buddy", "Golden Retriever");
myDog.eat();
myDog.bark();

在这个例子中,Dog 类继承了 Animal 类,因此它可以使用父类 Animalname 属性和 eat 方法。Dog 类还可以添加新的属性和方法,如 breedbark

4. 混合使用接口和类

在实际开发中,你可能会混合使用接口和类来实现复杂的类型继承关系。

typescript 复制代码
interface Animal {
    name: string;
    eat(): void;
}

interface DogInterface extends Animal {
    breed: string;
    bark(): void;
}

class AnimalBase implements Animal {
    name: string;

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

    eat() {
        console.log(`${this.name} is eating.`);
    }
}

class Dog extends AnimalBase implements DogInterface {
    breed: string;

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

    bark() {
        console.log(`${this.name} is barking.`);
    }
}

const myDog = new Dog("Buddy", "Golden Retriever");
myDog.eat();
myDog.bark();

在这个例子中,DogInterface 接口扩展了 Animal 接口,AnimalBase 类实现了 Animal 接口,而 Dog 类则继承了 AnimalBase 类并实现了 DogInterface 接口。这种混合使用接口和类的方式可以提供更高的灵活性和可维护性。

通过这些示例,你可以看到 TypeScript 提供了多种方式来实现类型的继承,以满足不同的开发需求。

相关推荐
能来帮帮蒟蒻吗2 天前
VUE3 -综合实践(Mock+Axios+ElementPlus)
前端·javascript·vue.js·笔记·学习·ajax·typescript
菜鸟una2 天前
【taro3 + vue3 + webpack4】在微信小程序中的请求封装及使用
前端·vue.js·微信小程序·小程序·typescript·taro
struggle20253 天前
continue通过我们的开源 IDE 扩展和模型、规则、提示、文档和其他构建块中心,创建、共享和使用自定义 AI 代码助手
javascript·ide·python·typescript·开源
Bl_a_ck3 天前
【React】Craco 简介
开发语言·前端·react.js·typescript·前端框架
Bl_a_ck4 天前
开发环境(Development Environment)
开发语言·前端·javascript·typescript·ecmascript
菜鸟una4 天前
【layout组件 与 路由镶嵌】vue3 后台管理系统
前端·vue.js·elementui·typescript
浪裡遊5 天前
Typescript中的对象类型
开发语言·前端·javascript·vue.js·typescript·ecmascript
从味书5 天前
安装typescript时,npm install -g typescript报错
javascript·typescript·npm
風吹过6 天前
A* (AStar) 寻路
typescript·cocos2d
geovindu7 天前
vue3: pdf.js 2.16.105 using typescript
javascript·vue.js·typescript·pdf