美文网首页
TypeScript基础总结-类

TypeScript基础总结-类

作者: 葶寳寳 | 来源:发表于2019-10-29 21:11 被阅读0次

1.继承:派生类中有构造函数时,必须执行super(),会调用基类的构造方法,而且得在构造函数访问this的属性之前。

class Animal {
    name: string;
    constructor(thename: string) {
        this.name = thename;
    }
    move(distance: number) {
        console.log(this.name + ': ' + distance);
    }
}
 
class Cat extends Animal {
    name: string;
    constructor(name: string) {
        super(name);   
        this.name = name; // 在此之前要调用super()
    }
    move(distance: number) {
        console.log(distance);
        super.move(distance);
    }
} 

class Dog extends Animal {
    constructor(name: string) {
        super(name)
    }
    move(distance: number) {
        console.log(distance);
        super.move(distance);
    }
}

let tom = new Cat('tom');
let tim: Animal = new Dog('tim');

tom.move(12); //12 tom: 12
tim.move(10);  // 10 tim: 10

2.属性类型:以上🌰中的类属性默认为public
当属性为protectedprivate时: 派生类(子类)不能访问private属性,可以访问protected属性。这两种类型的属性均不能在类外边访问。

class Animal {
    private name: string;
    protected age: number
    constructor(thename: string) {
        this.name = thename;
    }
    move(distance: number) {
        console.log(this.name + ': ' + distance)
    }
}
 
class Cat extends Animal {
    constructor(name: string) {
        super(name);   
    }
    move(distance: number) {
        console.log(this.name);  // error: Property 'name' is private and only accessible within class 'Animal'.
        console.log(this.age);
        super.move(distance);
    }
} 

let tom  = new Cat('tom');
tom.name; // error: Property 'name' is private and only accessible within class 'Animal'.

当比较带有privateprotected类型的成员属性时, 如果其中一个类型里包含一个private成员,那么只有当另外一个类型中也存在这样一个private成员, 并且它们都是来自同一处声明(类)时,我们才认为这两个类型是兼容的。 对于 protected成员也使用这个规则。

3.readonly:设置属性为只读,必须在声明或构造函数中被初始化。
可通过给构造函数参数前加限定符来声明和初始化属性:

class Animal {
  readonly name: string;
  constructor(readonly age: number) {
     this.name = 'ltt';
  }
}

4.存取器(gettersetter):

class Animal {
    private name: string;
    get getName() {
        return this.name;
    }
    set getName(name: string) {
        this.name = name;
    }
}
let  cat  = new Animal();
cat.name = 'Tom';
console.log(cat.name);

运行时可能会报错,官方文档要求:存取器要求你将编译器设置为输出ECMAScript 5或更高。不支持降级到ECMAScript 3。其次,只带有get不带有 set的存取器自动被推断为 readonly

报错.png
所以,运行时需要加-t es5参数:tsc -t es5 class.ts

5.静态属性(static):存在类本身上边而不是实例上。每个实例想访问属性时,都需要在属性前加类名。

class Animal {
    static age: number;
    constructor(age: number) {
       Animal.age = age;
    }
    getAge() {
       console.log(Animal.age);
    }
}

let a = new Animal(1);
a.getAge();
a.age;   //  error: Property 'age' is a static member of type 'Animal'

6.抽象类(abstract):抽象类不能被直接实例化,,也可以包含成员的实现细节。抽象类中的抽象方法必须在派生类中被实现。

abstract class Animal {
    abstract getAge();
}

// let an = new Animal();   // error: Cannot create an instance of an abstract class.

class Cat extends Animal {
    getAge() {
        console.log('age');
    }
}

let an = new Cat();

思考:类与接口的区别。

相关文章

  • TypeScript基础总结-类

    1.继承:派生类中有构造函数时,必须执行super(),会调用基类的构造方法,而且得在构造函数访问this的属性之...

  • TypeScript 基础 — 类

    TypeScript 向 JavaScript 类添加类型和可访问性修饰符。 类成员的类型 类的成员(属性和方法)...

  • typescript 基础总结

    先提一嘴:TypeScript 是结构类型系统,类型之间的对比只会比较它们最终的结构,而会忽略它们定义时的关系。 ...

  • TypeScript基础总结-基础类型

    一.基础类型1.布尔值(boolean):同js,true/false2.数字(number):同js,浮点数。可...

  • TypeScript基础入门 - 类 - 抽象类

    转载 TypeScript基础入门 - 类 - 抽象类 项目实践仓库 为了保证后面的学习演示需要安装下ts-nod...

  • TypeScript基础总结-函数

    1.函数类型:三种定义函数的方式: 2.类型推导:赋值语句一边指定返回值类型,另一边未指定,ts编译器会自动识别出...

  • TypeScript基础总结-接口

    是什么?TODO 用法先看个简单的? 在这个?中,定义了Student接口和getStudent方法,规定getS...

  • TypeScript基础学习总结

    准备工作: 环境安装:npm install -g typescript查看:tsc-v出现版本号说明安装成功安装...

  • TypeScript基础入门 - 类 - 继承

    转载 TypeScript基础入门 - 类 - 继承 项目实践仓库 为了保证后面的学习演示需要安装下ts-node...

  • TypeScript基础入门 - 类 - 简介

    转载地址 TypeScript基础入门 - 类 - 简介 项目实践仓库 为了保证后面的学习演示需要安装下ts-no...

网友评论

      本文标题:TypeScript基础总结-类

      本文链接:https://www.haomeiwen.com/subject/wcavvctx.html