fe-interview icon indicating copy to clipboard operation
fe-interview copied to clipboard

[TypeScript] 第1511天 请说说你对TypeScript中类的理解

Open haizhilin2013 opened this issue 2 years ago • 2 comments

第1511天 请说说你对TypeScript中类的理解

3+1官网

我也要出题

haizhilin2013 avatar Jun 04 '23 20:06 haizhilin2013

Typescript中的类是一种面向对象编程的重要概念,它允许你以一种面向对象的方式组织代码,定义属性,方法和构造函数。实现封装,继承和多态等面向对象的特性。与javascript不同,Typescript提供了强类型的类支持,可以通过类实现更严格的类型检查和更安全的代码。

  1. 类可以通过 class 关键字进行声明,然后通过 new 关键字实例化为对象。
  2. 类可以拥有构造函数,通过 constructor 方法来初始化对象的属性和状态。构造函数会在创建对象时自动调用。
  3. 类可以包含属性和方法,用于描述对象的状态和行为。属性可以包含成员属性、静态属性等。
  4. TypeScript 提供了访问修饰符(public、private、protected)来限制对类的成员的访问。
  5. 类可以通过 extends 关键字实现继承,子类可以继承父类的属性和方法,并且可以重写父类的方法。
  6. 抽象类(abstract )用于描述不能被实例化的类,通常用于作为其他类的基类。抽象类可以包含抽象方法,子类必须实现这些抽象方法。
  7. 类可以实现一个或多个接口,通过 implements 关键字实现接口,确保类符合接口的契约。

hemengzhao avatar Sep 26 '23 02:09 hemengzhao

在TypeScript中,类(Class)是面向对象编程的核心概念之一。它们允许你定义对象的结构(包括属性和方法),并提供了继承、封装、多态等面向对象编程的特性。TypeScript的类基于ECMAScript 2015(ES6)的类,并增加了静态类型检查和一些额外的功能。

基本语法

TypeScript中的类使用class关键字来定义:

class Person {
    // 属性
    name: string;
    age: number;

    // 构造函数
    constructor(name: string, age: number) {
        this.name = name;
        this.age = age;
    }

    // 方法
    greet() {
        console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
    }
}

创建实例

你可以使用new关键字来创建类的实例:

const person = new Person("Alice", 30);
person.greet();  // 输出: Hello, my name is Alice and I am 30 years old.

访问修饰符

TypeScript支持三种访问修饰符:publicprivateprotected

  • public(默认):可以在任何地方访问。
  • private:只能在类的内部访问。
  • protected:可以在类的内部和子类中访问。
class Animal {
    public name: string;
    private age: number;
    protected species: string;

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

    public getAge(): number {
        return this.age;
    }

    protected getSpecies(): string {
        return this.species;
    }
}

class Dog extends Animal {
    constructor(name: string, age: number) {
        super(name, age, "Dog");
    }

    public getSpeciesInfo() {
        console.log(`This is a ${this.getSpecies()}`);
    }
}

const dog = new Dog("Buddy", 5);
console.log(dog.name);  // 输出: Buddy
console.log(dog.getAge());  // 输出: 5
dog.getSpeciesInfo();  // 输出: This is a Dog
// console.log(dog.age);  // 错误: 'age' 是私有的
// console.log(dog.species);  // 错误: 'species' 是受保护的

静态成员

类的静态成员(属性和方法)属于类本身,而不是类的实例。它们使用static关键字定义。

class MathUtil {
    static PI: number = 3.14;

    static calculateCircumference(diameter: number): number {
        return this.PI * diameter;
    }
}

console.log(MathUtil.PI);  // 输出: 3.14
console.log(MathUtil.calculateCircumference(10));  // 输出: 31.4

抽象类

抽象类是不能被实例化的类,它们通常作为其他类的基类。抽象类可以包含抽象方法,这些方法没有实现,必须在子类中实现。

abstract class Shape {
    abstract area(): number;

    describe() {
        console.log(`This shape has an area of ${this.area()} square units.`);
    }
}

class Rectangle extends Shape {
    constructor(public width: number, public height: number) {
        super();
    }

    area(): number {
        return this.width * this.height;
    }
}

const rectangle = new Rectangle(10, 20);
rectangle.describe();  // 输出: This shape has an area of 200 square units.

类和接口

类可以实现接口,从而强制类遵循接口的结构。

interface Drivable {
    start(): void;
    drive(speed: number): void;
}

class Car implements Drivable {
    start() {
        console.log("Car started");
    }

    drive(speed: number) {
        console.log(`Driving at ${speed} km/h`);
    }
}

const car = new Car();
car.start();  // 输出: Car started
car.drive(100);  // 输出: Driving at 100 km/h

类的总结

  • 定义和实例化:使用class关键字定义类,使用new关键字创建实例。
  • 访问修饰符:包括publicprivateprotected,控制类成员的可见性。
  • 静态成员:使用static关键字定义,属于类本身而不是实例。
  • 抽象类:不能被实例化,通常作为基类使用,包含抽象方法。
  • 接口实现:类可以实现接口,确保类遵循接口的结构。

通过这些特性,TypeScript中的类提供了强大的面向对象编程功能,同时通过静态类型检查提供了更高的代码安全性和可维护性。

llccing avatar Jun 26 '24 06:06 llccing