work hard work smart

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

Typescript学习总结之接口

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

接口

用来建立某种代码约定,使得其他开发者在调用某个方法或者创建新的类时必须遵守接口所定义的代码约定

1. 接口声明属性

interface IStudent { 
    name: string;
    age: number;
}

class Student {

    constructor(public iStudeng: IStudent) { 
    }

}

var s1 = new Student({name:"zhangsan",age: 30})

 

2. 接口声明方法

interface IStudent { 
    say();
}

class Student implements IStudent {
    say() {
        console.log("i am saying");
     }
}

class HightStudent implements IStudent {
    say() {
        console.log("high school i am saying");
     }
}

var s1 = new Student()

  继承接口的类要实现接口中的方法,如上面的say方法