返回

TypeScript实例解析:从入门到精通

前端

TypeScript中的类转换成ES5的代码后:

  • 类中的所有方法都会被转换成函数,并且这些函数都将具有一个prototype属性。
  • 类的所有属性都会被转换成类的实例属性。
  • TypeScript中的继承是通过extends来实现的。
  • TypeScript中类的实例可以访问父类的属性和方法。
  • TypeScript中类的属性可以通过public、private和protected三种修饰符来控制访问权限。

TypeScript中类的继承:

  • TypeScript中的类可以继承自其他类,并且子类可以访问父类的属性和方法。
  • TypeScript中的继承是通过extends关键字来实现的。
  • 子类可以覆盖父类的方法。
  • 子类可以扩展父类的属性。

TypeScript中公共,私有与受保护的修饰符

  • public:表示属性或方法可以在类的内部和外部访问。
  • private:表示属性或方法只能在类的内部访问。
  • protected:表示属性或方法只能在类的内部和其子类中访问。

实例1:TypeScript中的类

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

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

    public greet() {
        console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
    }
}

let person = new Person("John", 30);
person.greet();

在这个实例中,我们定义了一个名为Person的类。该类有两个属性:name和age。name属性是私有的,这意味着它只能在类的内部访问。age属性是公共的,这意味着它可以在类的内部和外部访问。Person类还有一个构造函数,该构造函数在创建Person类的实例时被调用。构造函数有两个参数:name和age。当我们创建Person类的实例时,我们将name和age作为参数传递给构造函数。在构造函数中,我们将name和age分别赋值给name和age属性。

实例2:TypeScript中的类的继承

class Employee extends Person {
    private salary: number;

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

    public getSalary() {
        return this.salary;
    }
}

let employee = new Employee("John", 30, 10000);
console.log(employee.getSalary());

在这个实例中,我们定义了一个名为Employee的类,该类继承自Person类。Employee类有一个私有的salary属性,该属性只能在类的内部访问。Employee类还有一个构造函数,该构造函数在创建Employee类的实例时被调用。构造函数有三个参数:name、age和salary。当我们创建Employee类的实例时,我们将name、age和salary作为参数传递给构造函数。在构造函数中,我们将name和age分别赋值给name和age属性,并将salary赋值给salary属性。Employee类还有一个getSalary方法,该方法可以返回salary属性的值。

实例3:TypeScript中公共,私有与受保护的修饰符

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

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

    public greet() {
        console.log("Hello, my name is " + this.name + " and I am " + this.age + " years old.");
    }

    public getSalary() {
        return this.salary;
    }
}

class Employee extends Person {
    public department: string;

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

    public getDepartment() {
        return this.department;
    }
}

let person = new Person("John", 30, 10000);
person.greet();

let employee = new Employee("John", 30, 10000, "Sales");
console.log(employee.getSalary());
console.log(employee.getDepartment());

在这个实例中,我们定义了一个名为Person的类,该类有两个属性:name和age。name属性是公共的,这意味着它可以在类的内部和外部访问。age属性是私有的,这意味着它只能在类的内部访问。Person类还有一个构造函数,该构造函数在创建Person类的实例时被调用。构造函数有两个参数:name和age。当我们创建Person类的实例时,我们将name和age作为参数传递给构造函数。在构造函数中,我们将name和age分别赋值给name和age属性。

Person类还有一个greet方法,该方法可以输出"Hello, my name is " + this.name + " and I am " + this.age + " years old."。

Person类还有一个getSalary方法,该方法可以返回salary属性的值。

Employee类继承自Person类。Employee类有一个公共的department属性,该属性可以在类的内部和外部访问。Employee类还有一个构造函数,该构造函数在创建Employee类的实例时被调用。构造函数有四个参数:name、age、salary和department。当我们创建Employee类的实例时,我们将name、age、salary和department作为参数传递给构造函数。在构造函数中,我们将name、age和salary分别赋值给name、age和salary属性,并将department赋值给department属性。

Employee类还有一个getDepartment方法,该方法可以返回department属性的值。

在这个实例中,我们创建了一个Person类的实例和一个Employee类的实例。我们调用Person类的greet方法和Employee类的getSalary方法和getDepartment方法来输出结果。