小白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 方法的实现。通过这种方式,抽象类允许我们定义一个通用的框架,将共同的代码抽取到一个基类中,同时允许子类提供特定的实现。

抽象类的作用主要有:

  • 封装:将通用的代码封装在抽象类中,减少代码重复。
  • 强制规范:抽象类可以强制子类实现特定的方法和属性。
相关推荐
晓杰'5 小时前
从0到1实现Balatro游戏后端(5):得分计算与单局结算流程实现
后端·typescript·node.js·游戏开发·项目实战·nestjs·webscoket
追光者♂5 小时前
【测评系列3】CSDN AI数字营销实测体验官:测试 开源项目——Superpowers 游戏引擎从零开发实战指南
人工智能·深度学习·机器学习·typescript·开源·游戏引擎·superpowers
ct9785 小时前
TypeScript 中的泛型
前端·javascript·typescript
烛衔溟18 小时前
TypeScript 类的类型 —— 作为类型使用
javascript·ubuntu·typescript
向上的车轮19 小时前
NestJS 的优秀替代框架——系统化选型指南(2026视角)
nestjs
阿隅21 小时前
TS 深度解析:同为 ? 可选语法,为什么赋值一错一对?类类型与this绑定底层拆解
typescript
Patrick_Wilson1 天前
前端解析接口数据,到底该不该信任后端?聊聊「防御性编程」与「类型契约」的边界
架构·typescript·代码规范
姓蔡小朋友1 天前
TypeScript数据类型
javascript·ubuntu·typescript
烛衔溟1 天前
TypeScript 高级类型与工具类型全解
javascript·ubuntu·typescript
UaoN2 天前
Vibe Coding 时代,为什么 Tailwind + Shadcn/ui 正在成为现代前端的默认答案
react.js·typescript