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

抽象类的作用主要有:

  • 封装:将通用的代码封装在抽象类中,减少代码重复。
  • 强制规范:抽象类可以强制子类实现特定的方法和属性。
相关推荐
阿蒙Amon8 小时前
TypeScript学习-第7章:泛型(Generic)
javascript·学习·typescript
We་ct19 小时前
LeetCode 289. 生命游戏:题解+优化,从基础到原地最优
前端·算法·leetcode·矩阵·typescript
We་ct21 小时前
LeetCode 383. 赎金信:解题思路+代码解析+优化实战
前端·算法·leetcode·typescript
D11_21 小时前
[特殊字符]️ 5379工具箱 - 全部网站链接汇总
服务器·百度·阿里云·typescript·编辑器
敲敲了个代码1 天前
从N倍人力到1次修改:Vite Plugin Modular 如何拯救多产品前端维护困境
前端·javascript·面试·职场和发展·typescript·vite
VT.馒头2 天前
【力扣】2625. 扁平化嵌套数组
前端·javascript·算法·leetcode·职场和发展·typescript
San30.2 天前
从零构建坚固的前端堡垒:TypeScript 与 React 实战深度指南
前端·react.js·typescript
VT.馒头2 天前
【力扣】2694. 事件发射器
前端·javascript·算法·leetcode·职场和发展·typescript
止观止2 天前
像三元表达式一样写类型?深入理解 TS 条件类型与 `infer` 推断
前端·typescript
We་ct2 天前
LeetCode 73. 矩阵置零:原地算法实现与优化解析
前端·算法·leetcode·矩阵·typescript