typescript 接口 interface
typescript接口 interface
2023-09-11 14:15:29 时间
代码:
// 接口:行为的抽象 // 一、对class类的约束 // 接口定义 // 打印机 interface Iprinter { Printing(msg:string):string; } interface Imessage { getmsg():string; } // 实现接口/实现多个接口 class colorprinter implements Iprinter,Imessage { Printing(msg:string):string{ return `打印${msg}成功!` } getmsg():string{ return "惠普HP10000"; } } let p1 = new colorprinter(); let val = p1.Printing("简历"); console.log(p1.getmsg()) console.log(val) // 1.实现接口,要实现里面的内容 // 2.定义接口的时候,只定义声明即可,不包含具体内容 // 二、对函数的约束 interface Imyfunction { (a:string,b:number):boolean; } let fun1:Imyfunction; fun1 = function(a:string,b:number):boolean { return false; } // 三、对数组 interface Istuarr { [index:number]:string; } let arr1:Istuarr; arr1 = ['aa','bb'] console.log(arr1[0]) // 四、关于json的约束 interface Idata { name:string, readonly age:number, // 只读属性 email?:string // 可选属性 } function showdata(n:Idata) { // n.age = 18; console.log(JSON.stringify(n)) } showdata({name:"张三",age: 19}) // 接口继承
.
相关文章
- 【Vue】组件template中提示“TypeScript intellisense is disabled on template. To enable, configure `“jsx“: ”错误
- 快速上手typescript(进阶篇)
- 初始化一个React项目(TypeScript环境)
- TypeScript 迭代器(iterator)和生成器(generator)
- Webpack 3.0 前端工程化(编译ES6/ES7、编译TypeScript、打包提取公共代码、代码分割和懒加载)
- TypeScript目录
- create-react-app添加对TypeScript支持
- typescript 接口、类、泛型、枚举、断言的简单使用
- 搭建typescript练习环境
- 浅析TypeScript中const和readonly的区别、枚举和常量枚举的区别以及关于typescript中枚举的相关知识
- TypeScript “==“ 和 “===“区别
- TypeScript:接口