小白NestJS官网文档之类提供者:useClass,拓展抽象类

ConfigService 抽象类

csharp 复制代码
// config.service.ts
export abstract class ConfigService {
  abstract get(key: string): string; // 抽象方法,子类必须实现
}

通常使用抽象类来代表接口

DevelopmentConfigService 实现类

scala 复制代码
// development-config.service.ts
import { ConfigService } from './config.service';

export class DevelopmentConfigService extends ConfigService {
  private readonly envConfig: { [key: string]: string } = {
    // 默认开发环境配置
    DB_HOST: 'localhost',
    DB_PORT: '5432',
  };

  get(key: string): string {
    return this.envConfig[key];
  }
}

ProductionConfigService 实现类

scala 复制代码
// production-config.service.ts
import { ConfigService } from './config.service';

export class ProductionConfigService extends ConfigService {
  private readonly envConfig: { [key: string]: string } = {
    // 默认生产环境配置
    DB_HOST: 'prod-db-host',
    DB_PORT: '5432',
  };

  get(key: string): string {
    // 在生产环境中,通常会从环境变量或其他安全存储中获取配置
    return process.env[key] || this.envConfig[key];
  }
}

拓展抽象类

在编程中,抽象类是一种特殊类型的类,它不能被直接实例化,只能作为其他类的基类。抽象类的目的是为一组子类提供一个公共的基本结构。它可能包含以下内容:

  1. 抽象方法:这些方法没有具体的实现(即没有方法体)。它们必须在继承抽象类的具体子类中被实现。
  2. 具体方法:这些方法在抽象类中已经具有实现。子类可以直接使用这些方法,也可以根据需要重写它们。
  3. 属性:抽象类可以包含属性。这些属性可能是具体的(有初始值)或者抽象的(没有初始值,期望子类提供)。
  4. 构造函数:抽象类可以有构造函数,尽管抽象类本身不能被实例化,但构造函数可以被子类调用。
typescript 复制代码
abstract class Animal {
  // 抽象属性,子类需要提供该属性的具体值
  abstract get species(): string;

  // 具体属性
  age: number;

  // 使用构造函数来设定属性的初始值
  constructor(age: number) {
    this.age = age;
  }

  // 抽象方法,需要在子类中具体实现
  abstract makeSound(): void;

  // 具体方法,可以直接被所有动物子类使用
  eat() {
    console.log(`This ${this.species} is eating.`);
  }

  // 具体方法,输出动物信息
  printInfo() {
    console.log(`This is a ${this.species}, and it is ${this.age} years old.`);
  }
}

// 具体子类 Dog 继承自 Animal
class Dog extends Animal {
  // 提供 species 属性的具体值
  get species(): string {
    return 'dog';
  }

  // 实现抽象方法 makeSound
  makeSound(): void {
    console.log('Woof!');
  }
}

// 具体子类 Cat 继承自 Animal
class Cat extends Animal {
  // 提供 species 属性的具体值
  get species(): string {
    return 'cat';
  }

  // 实现抽象方法 makeSound
  makeSound(): void {
    console.log('Meow!');
  }
}

// 使用具体子类创建实例
const dog = new Dog(5); // 创建一个年龄为 5 的狗
dog.printInfo(); // 输出: This is a dog, and it is 5 years old.
dog.makeSound(); // 输出: Woof!
dog.eat(); // 输出: This dog is eating.

const cat = new Cat(3); // 创建一个年龄为 3 的猫
cat.printInfo(); // 输出: This is a cat, and it is 3 years old.
cat.makeSound(); // 输出: Meow!
cat.eat(); // 输出: This cat is eating.

SpeciesmakeSound 是抽象的,这意味着它们需要在每个具体的动物子类中被实现。eatprintInfo 方法则是具体的,所有的动物都可以使用这个方法。

DogCatAnimal 的具体子类,它们分别提供了 species 的值和 makeSound 方法的实现。通过这种方式,抽象类允许我们定义一个通用的框架,将共同的代码抽取到一个基类中,同时允许子类提供特定的实现。

抽象类的作用主要有:

  • 封装:将通用的代码封装在抽象类中,减少代码重复。
  • 强制规范:抽象类可以强制子类实现特定的方法和属性。
相关推荐
yume_sibai6 小时前
大屏数据可视化 - 边框红绿呼吸灯实现详解
前端·信息可视化·typescript
孟无岐15 小时前
LayaAir IDE 插件开发入门:用 TypeScript 给编辑器加一个自己的面板
typescript·游戏开发·工程化·编辑器扩展·layaair·ide 插件
shakingWaves17 小时前
better auth worker on cloudflare
typescript·cloudflare·auth·better-auth
退休倒计时1 天前
【每日一题】LeetCode 437. 路径总和 III TypeScript
算法·leetcode·typescript
柯克七七1 天前
给老项目加了 TypeScript,本来只想自己爽,结果全公司代码审查标准被我抬高了
前端·vue.js·typescript
yume_sibai2 天前
高德地图骑行导航 API 介绍与使用方法
前端·typescript
子兮曰2 天前
TypeScript 7.0 RC 深度解读:Go 重写完成,10 倍提速,编译器的「奇点时刻」
前端·后端·typescript
BAIGAOa2 天前
不止 React:我开源了一个框架无关的终端键盘引擎,Vue/Svelte 都能用
typescript
小蚂蚁i2 天前
把 TypeScript 内置工具类型彻底搞懂,顺便自己封几个好用的
前端·typescript
爸爸6192 天前
07 ArkTS 基础类型与接口定义:从 TypeScript 到鸿蒙的类型升级
javascript·华为·typescript·harmonyos·鸿蒙系统