work hard work smart

专注于Java后端开发。 不断总结,举一反三。
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Typescript学习总结之泛型

Posted on 2018-02-14 21:44  work hard work smart  阅读(133)  评论(0编辑  收藏  举报

泛型:

参数化的类型,一般用来限制结合的内容

class Student {

    constructor(public name: string) { 
    }

    say() { 
        console.log(this.name + " saying");
    }
}

class HighSchoolStudent extends Student { 

    constructor(name: string, no: string) {
        super(name)
     }
    no:string;
    study() {
        super.say();
     }
}

var students: Array<Student> = [];
students[0] = new Student("zhangsan");
students[1] = new HighSchoolStudent("lisi","06169010");

泛型 var students: Array<Student> = []; 用来定义数组只用是Student元素