1、类类型接口
class Dog{ eat(){ console.log('骨头'); } run(){ console.log('狗跑步'); }}class Cat{ eat(){ console.log('鱼'); } run(){ console.log('猫跑步'); }}//只要是动物类,都需有eat和run方法
// 类类型接口interface Animal{ eat():void; run():void;}/* 接口就是一套规范,当很多的类型具备相同的特征或者属性,我们可以将这些特征属性单独抽离成一个接口,让这些类对这个接口(规范)进行实现 */class Dog implements Animal{ eat():void{ console.log('骨头'); } run():void{ console.log('狗跑'); }}class Cat implements Animal{ eat():void{ console.log('鱼'); } run():void{ console.log('猫跑'); }}class Fish implements Animal{ eat():void{ console.log('鱼食'); } run():void{ console.log('游'); }}/* 1、那么一个类实现某个接口以后,必须对接口中的属性和方法进行重写*/
2、属性接口
// 属性接口:/* 对方法的参数的约束 */interface Params{ url:string method:string // ?:表示这个属性可以有也可以没有 data?:Object}function http(d:Params):void{ console.log(d);}http({ url:"top250", method:"get", data:{ limit:5 }})http({ url:"top250", method:"post"})
3、函数类型接口
interface encrypt{ // 设置函数的参数和返回值的类型 (key:string,value:string):string}var md5:encrypt = function(key:string,value:string):string{ return key+value}console.log(md5("key","value"));
4、接口的继承
interface Animal{ name:string age:number}interface Dog extends Animal{ eat(str:string):void}class WolfDog implements Dog{ name:string age:number eat(str:string):void{ console.log(str); } constructor(name:string,age:number){ this.name = name this.age = age }}var w:WolfDog = new WolfDog("狗",7)