小白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.

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

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

抽象类的作用主要有:

  • 封装:将通用的代码封装在抽象类中,减少代码重复。
  • 强制规范:抽象类可以强制子类实现特定的方法和属性。
相关推荐
zhousenshan1 天前
TypeScript 入门到精通
typescript
花旗蜕变1 天前
前端转 NestJS 全栈实践:从表单页面到微信业务系统
前端·typescript·nestjs
艺杯羹1 天前
Anthropic突袭发布Claude Code Mods!用TypeScript编写智能体中间件:告别AI误删与失控
人工智能·typescript·系统架构
FYKJ_20104 天前
SSM校园失物招领系统41452-计算机课程设计、毕业设计
java·vue.js·spring boot·python·mysql·typescript·spark
tiankongdeyige5 天前
游戏战斗系统学习第2篇:有限状态机 (FSM)
学习·游戏·typescript
FYKJ_20105 天前
django电影推荐系统55066-计算机课程设计、毕业设计
vue.js·spring boot·python·mysql·typescript·spark·django
进击的明明5 天前
TypeScript速通笔记(下)
前端·面试·typescript
愛芳芳5 天前
element-plus+vite+vue3+pinpa+mock+typeScript搭建经典的电商管理后台
前端·javascript·typescript
miss5 天前
从零做一个可视化规则引擎:Vue3 递归条件树 + 双引擎结果对比
前端·vue.js·typescript
seowmt6 天前
结构化数据和自然语言描述哪个更管用?答案分场景
人工智能·google·typescript·go语言·schema