接口就是在面向对象编程中的一种规范的定义,定义行为和动作,起到一种限制和规范代码的作用,如果有学过c#和java这类面向对象的语言的话就非常容易懂了

1、属性接口

  1. //对类属性的约束
  2. interface Person{
  3. name:string;
  4. age:number
  5. }

2、对函数的约束

  1. interface Memo{ //对函数的约束
  2. (value:number,name:string):void
  3. }

3、对数组的约束

  1. interface Arr{ //定义数组
  2. [index:number]:string //index:索引值,
  3. }
  4. let arr:Arr=['ddd'] //value为string

4、对象的约束

  1. interface Obj{ //定义对象
  2. [index:string]:string //index:索引值,
  3. }
  4. let ob:Obj={name:'123'} //value为string

5、接口的实现

  1. interface Anmails{
  2. name:string;
  3. eat(name:string):void
  4. }
  5. class Dog implements Anmails{ //implements关键字 为‘实现’ 相当于class Dog 实现Anmails 接口
  6. name:string;
  7. constructor(name:string){
  8. this.name=name
  9. }
  10. eat(name:string){
  11. console.log(this.name)
  12. }
  13. }

6、接口的继承

  1. //接口的继承
  2. interface Anmails{
  3. eat(food:string):string
  4. }
  5. interface Cat extends Anmails{
  6. runing():void
  7. }
  8. class buleCat implements Cat{
  9. name:string
  10. constructor(name:string){
  11. this.name=name
  12. }
  13. eat(food:string){
  14. return this.name+'吃'+food
  15. }
  16. runing(){
  17. console.log( this.name+'在跑');
  18. }
  19. }
  20. var xiaoke = new buleCat('小可');
  21. xiaoke.eat('猫粮')
  22. xiaoke.runing()