interface Girll{ name:string; age:number; bust:number; } const girl={ name:'大脚', age:18, bust:100 } const screenResume2=( girl:Girl)=>{ girl.age<24 && girl.bust>=90 && console.log(girl.name+'进入面试') girl.age>24 || girl.bust<90 && console.log(girl.name+'你被淘汰') } const getResume2=( girl:Girl)=>{ console.log(girl.name+'年龄是:'+girl.age) console.log(girl.name+'胸围是:'+girl.bust) }
接口非必选定义
interface Girl { name : string; age : number; bust : number; waistline ?: number; }
const getResume=( girl:Girl)=>{ console.log(girl.name+'年龄是:'+girl.age) console.log(girl.name+'胸围是:'+girl.bust) girl.waistline && console.log(girl.name+'腰围是:'+girl.waistline) }
可以写任意参数
interface Girl{ name:string; age:number; bust:number; waistline?:number; [propname:string]:any //这个的意思是,属性的名字是字符串类型,属性的值可以是任何类型。 say():string //必须有string的返回值 } const girl={ name:'大脚', age:18, bust:94, waistline:21, sex:'女', say(){ return '欢迎光临 ,红浪漫洗浴!!' } }
接口和类的约束
class xiaojiejie implements Girl{ name='liuying' age=18 bust=100 say(){ return '12312' } }
接口间的继承
// 接口间的继承 interface Teacher extends Girl{ teach():string }
const girl2={ name:'大脚', age:18, bust:94, waistline:21, sex:'女', say(){ return '欢迎光临 ,红浪漫洗浴!!' }, teach(){ return '我是一个老师' } }