如何在 TypeScript 中使用类和继承

2023年 10月 18日 27.1k 0

TypeScript是一种编程语言,它是JavaScript的超集。它通过添加静态类型、类、接口和模块等功能来扩展JavaScript

class Hero {
  name: string;
  age: number;

  constructor(name: string, age: number) {
    this.name = name;
    this.age = age;
  }

  say(): void {
    console.log(`my name is ${this.name}`);
  }
}

在上面的代码中,我们定义了一个名为Hero的类,它具有name和age属性,以及一个say方法。构造函数用于初始化对象的属性。

通过类的定义,我们可以创建多个具有相同属性和行为的对象,实现代码的复用和封装。

const Jieke = new Hero('jieke', 28)
Jieke.say();  // 输出:my name is jieke

继承和子类

在面向对象编程中,继承是一种重要的概念。通过继承,我们可以创建一个类的子类(也称为派生类),并继承其属性和方法。子类可以扩展或修改父类的功能,从而实现代码的重用和扩展

在TypeScript中,我们使用extends关键字来指定一个类继承自另一个类

class Superman extends Hero {
  heroId: string;

  constructor(name: string, age: number, heroId: string) {
    super(name, age);
    this.heroId = heroId;
  }

  skill(): void {
    console.log(`${this.name}_${this.heroId}.`);
  }
}

我们定义了一个名为Superman的子类,它继承自Hero父类。子类具有自己的属性heroId,并通过调用super关键字来调用父类的构造函数。

创建子类的实例与创建父类的实例类似:

const s1 = new Superman("Batman", 20, "12345");
s1.say();  // 输出:my name is Batman
s1.skill();  // 输出:Batman_12345

限制类的属性和方法的访问

在TypeScript中,我们可以使用访问修饰符来限制类的属性和方法的访问。以下是几个常用的访问修饰符:

  • public(默认):可以在类内部和外部访问。
  • private:只能在类内部访问。
  • protected:可以在类内部和子类中访问,但不能在类外部访问。

通过使用访问修饰符,我们可以控制类的成员的可见性,增强了封装性和安全性

class Person {
  public name: string;
  private age: number;
  protected gender: string;

  constructor(name: string, age: number, gender: string) {
    this.name = name;
    this.age = age;
    this.gender = gender;
  }

  say() {
    console.log(`Hi, my name is ${this.name}.`);
  }

  private sayAge() {
    console.log(`I am ${this.age} years old.`);
  }
}

class Student extends Person {
  constructor(name: string, age: number, gender: string) {
    super(name, age, gender);
  }

  sayGender() {
    console.log(`My gender is ${this.gender}.`);
  }
}

const person = new Person("Tom", 18, "male");
console.log(person.name); // "Tom"
console.log(person.age); // Error: Property 'age' is private and only accessible within class 'Person'.
console.log(person.gender); // Error: Property 'gender' is protected and only accessible within class 'Person' and its subclasses.

const student = new Student("Jane", 20, "female");
console.log(student.gender); // "female"

抽象类

在TypeScript中,我们还可以使用抽象类(abstract class)来定义一个不可实例化的基类。抽象类提供了一种模板,用于派生其他类,并定义了一些必须由子类实现的抽象方法。抽象类不能被直接实例化,只能被继承

以下是一个抽象类的示例:

abstract class Role {
    // 定义抽象属性
    abstract name:string; 
    abstract age:number;

    // 定义保护属性,每个继承子类都能继承并调用的属性
    protected lastPoint!: {x:number,y:number};
    protected status!: "run" | "risk" | "die";

    // 定义保护方法,每个继承子类都能继承并调用的方法
    protected getLastPoint (lastPoint:{x:number,y:number}) {
        this.lastPoint = lastPoint;
    }

    // 定义抽象方法,在子类中重写来执行不同的任务
    abstract attack():void;
}

继承抽象类的子类实例化对象通过调用同一抽象类规定的抽象方法,来实现了不同的操作,体现了类的多态

class antor extends Role {
    // 重写抽象属性
    name: string = "han";
    age: string = 20;

    // 重写抽象方法
    attack(): void {
        console.log("正在被攻击");
    }
    constructor () {
        super();
    }
}

相关文章

如何删除WordPress中的所有评论
检查WordPress服务器磁盘使用情况的7种简便方法(查找大文件和数据)
如何更改WordPress常量FS_METHOD
如何为区块编辑器、Elementor等构建WordPress文章模板
如何彻底地删除WordPress主题及相关内容
如何使用WordPress搭建一个内网

发布评论