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

抽象类的作用主要有:

  • 封装:将通用的代码封装在抽象类中,减少代码重复。
  • 强制规范:抽象类可以强制子类实现特定的方法和属性。
相关推荐
Momo__2 天前
TypeScript NoInfer<T>——精准控制泛型推断的工具类型
前端·typescript
退休倒计时3 天前
【每日一题】LeetCode 146. LRU 缓存 TypeScript
算法·leetcode·缓存·typescript
kyriewen4 天前
TypeScript 高级类型:我用 infer 写了一个类型安全的 EventBus,终于搞懂了泛型约束
前端·javascript·typescript
月光刺眼4 天前
Bun + TypeScript 后端入门:从类型约束到 LLM API 调用
后端·typescript
天蓝色的鱼鱼4 天前
Node.js 现在能直接跑 TypeScript 了,tsx 和 ts-node 还需要吗?
前端·typescript·node.js
Oo9204 天前
Bun:下一代 JavaScript/TypeScript 运行时,从入门到实践
typescript·bun
Asize5 天前
Bun + TypeScript 实战:从接口约束到 RESTful 路由设计
后端·typescript·代码规范
大家的林语冰5 天前
超越 TypeScript,Flow 强势回归,语法高仿 TS,功能更丰富,类型更安全!
前端·javascript·typescript
用户484526255825 天前
Bun 入门:Bun.serve 零依赖启动 HTTP 服务
typescript
刚子编程5 天前
从零开始:在 Windows 服务器上部署 Node.js 项目(小白实战教程)
服务器·nestjs·pm2·windowsserver·node.js部署·caddy反向代理