返回

TypeScript Class详解

前端

1. 类的定义

在TypeScript中,可以使用class来定义类。类的语法如下:

class ClassName {
    // 属性
    property1: type;
    property2: type;

    // 方法
    method1(): void {
        // 方法体
    }

    method2(): void {
        // 方法体
    }
}

其中,ClassName是类的名称,property1和property2是类的属性,method1和method2是类的方。

2. 类的属性

类的属性是用来对象的状态或特征的变量。属性可以是任何类型,包括基本类型(如number、string、boolean等)和引用类型(如数组、对象等)。

class Person {
    name: string;
    age: number;

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

在这个例子中,Person类有两个属性:name和age。name是字符串类型,age是数字类型。

3. 类的方

类的方是用来定义对象的行为的函数。方法可以是任何类型,包括void类型(不返回值)和非void类型(返回值)。

class Person {
    name: string;
    age: number;

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

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

在这个例子中,Person类有一个方法:greet()。greet()方法是一个void类型的方法,它不会返回值。

4. 类的继承

类的继承是指一个类可以继承另一个类的属性和方法。继承的语法如下:

class ChildClass extends ParentClass {
    // 子类特有的属性和方法
}

其中,ChildClass是子类,ParentClass是父类。子类可以继承父类的所有属性和方法,并且可以定义自己的属性和方法。

class Person {
    name: string;
    age: number;

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

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

class Student extends Person {
    studentId: number;

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

    study(): void {
        console.log(`I am studying.`);
    }
}

在这个例子中,Student类继承了Person类。Student类有两个属性:studentId和name,age。Student类有两个方法:study()和greet()。

5. 类的封装

类的封装是指将类的属性和方法隐藏起来,只允许通过类的公开接口来访问。封装的语法如下:

class ClassName {
    private property1: type;
    protected property2: type;
    public property3: type;

    // 方法
    private method1(): void {
        // 方法体
    }

    protected method2(): void {
        // 方法体
    }

    public method3(): void {
        // 方法体
    }
}

其中,private、protected和public是访问修饰符。private表示该属性或方法只能在类内部访问,protected表示该属性或方法只能在类内部和子类中访问,public表示该属性或方法可以在类内外访问。

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

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

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

class Student extends Person {
    private studentId: number;

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

    study(): void {
        console.log(`I am studying.`);
    }
}

在这个例子中,Person类定义了三个属性:name、age和gender。name是私有属性,只能在Person类内部访问。age是受保护的属性,只能在Person类内部和Student类内部访问。gender是公共属性,可以在Person类内外访问。

6. 类的抽象

类的抽象是指将类的属性和方法声明为抽象的,而不提供具体的实现。抽象的语法如下:

abstract class ClassName {
    abstract property1: type;
    abstract property2: type;

    abstract method1(): void;
    abstract method2(): void;
}

其中,abstract表示该属性或方法是抽象的。抽象的属性和方法不能在类内部实现,必须在派生类中实现。

abstract class Shape {
    abstract area(): number;
}

class Rectangle extends Shape {
    width: number;
    height: number;

    constructor(width: number, height: number) {
        this.width = width;
        this.height = height;
    }

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

class Circle extends Shape {
    radius: number;

    constructor(radius: number) {
        this.radius = radius;
    }

    area(): number {
        return Math.PI * this.radius ** 2;
    }
}

在这个例子中,Shape类定义了两个抽象方法:area()。Rectangle类和Circle类继承了Shape类,并实现了area()方法。

7. 类的多态性

类的多态性是指子类可以重写父类的方法,从而实现不同的行为。多态的语法如下:

class ParentClass {
    method(): void {
        // 方法体
    }
}

class ChildClass extends ParentClass {
    @Override
    method(): void {
        // 方法体
    }
}

其中,@Override表示子类的方法重写了父类的方法。

class Shape {
    abstract area(): number;
}

class Rectangle extends Shape {
    width: number;
    height: number;

    constructor(width: number, height: number) {
        this.width = width;
        this.height = height;
    }

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

class Circle extends Shape {
    radius: number;

    constructor(radius: number) {
        this.radius = radius;
    }

    area(): number {
        return Math.PI * this.radius ** 2;
    }
}

function calculateArea(shape: Shape) {
    return shape.area();
}

在这个例子中,Shape类定义了一个抽象方法:area()。Rectangle类和Circle类继承了Shape类,并实现了area()方法。calculateArea()函数可以接受任何Shape类的对象,并计算该对象的面积。