当前位置:网站首页>TypeScript接口
TypeScript接口
2022-06-28 05:11:00 【阿嚏阿嚏-】
TypeScript的核心原则之一是对值所具有的类型进行检查。
使用接口定义对象的类型,接口是对象属性和方法的描述
接口:是一种类型、一种规范、一个能力、一种约束
可选属性
- 可定义接口里的属性不一定必须,语法
属性?
interface IPerson {
firstName: string
lastName?: string //非必须属性
}
只读属性
- 可定义接口里的属性为只读,不可更改,语法
readonly 属性
interface IPerson {
readonly id: number //只读属性
firstName: string
lastName: string
}
函数类型
- 函数类型:定义一个接口,作为函数的类型
- 使用接口表示函数的类型,需要给接口定义一个调用签名
- 调用签名:像是一个只有参数列表和返回值类型的函数定义。参数列表中的每个参数都需要名字和类型
//定义一个接口,用来作为函数的类型
interface ISearchFunc {
//定义一个调用签名
(source: string, subStr: string): boolean
}
//定义一个函数,类型为上面的接口
const searchString: ISearchFunc = function (source: string, subStr: string): boolean{
return source.search(subStr) > -1
}
//调用函数
console.log(searchString('天上的风筝', '风')); //true
类类型
- 类类型:通过接口实现了类的类型,用来明确的强制一个类去符合某种契约
//定义一个接口
interface IFly {
fly() //该方法没有任何的实现
}
//定义一个类,其类型为上面定义的那个接口
class Person implements IFly {
//实现接口中的方法
fly() {
console.log('飞飞飞');
}
}
//实例化对象
const person = new Person
person.fly()
类和接口的关系不叫继承,叫实现
- 一个类可以实现多个接口
interface ISwim {
swim()
}
class Person2 implements IFly,ISwim {
fly() {
console.log('飞2');
}
swim() {
console.log('游泳2');
}
}
const person2 = new Person2
person2.fly()
person2.swim()
类可以实现一个接口,可以实现多个接口,注意接口的内容都要真正的实现
- 一个接口可以继承多个接口
interface IAll extends IFly, ISwim {
}
//定义一个类,直接实现继承后的接口
class Person3 implements IAll {
fly() {
console.log('飞3');
}
swim() {
console.log('游泳3');
}
}
const person3 = new Person3
person3.fly()
person3.swim()
总结:接口和接口之间叫继承(使用extends关键字),类和接口之间叫实现(使用implements关键字)
边栏推荐
- A guide to P2P network penetration (stun) for metartc5.0 programming
- Extjs library management system source code intelligent library management system source code
- 电源插座是如何传输电的?困扰小伙伴这么多年的简单问题
- 【SkyWalking】一口气学完分布式链路追踪SkyWalking
- 机器人学DH参数及利用matlab符号运算推导
- Informatics Orsay all in one 1360: strange lift
- 学习太极创客 — MQTT 第二章(六)MQTT 遗嘱
- gorm事务体验
- MySQL 45讲 | 05 深入浅出索引(下)
- 活性染料研究:Lumiprobe AF594 NHS 酯,5-异构体
猜你喜欢

二级造价工程师证书含金量到底有多高?看这些就知道了

CpG solid support research: lumiprobe general CpG type II

2022 high altitude installation, maintenance and removal examination questions and answers

Lumiprobe cell imaging analysis: PKH26 cell membrane labeling kit

机器人学DH参数及利用matlab符号运算推导

Voltage mode and current mode control of switching power supply

2022 low voltage electrician examination questions and answers

How high is the gold content of grade II cost engineer certificate? Just look at this

Latest Windows version 5.0.14 of redis

Have you finished the examination of level II cost engineer? There are also qualification regulations!
随机推荐
Wireless sensor network learning notes (I)
程序员-放羊娃
Simple usage of GSAP
Learn Taiji Maker - mqtt Chapter 2 (IV) esp8266 reserved message application
IP datagram sending and forwarding process
Gorm transaction experience
[JVM series] JVM tuning
[microservices openfeign] openfeign quick start service invocation based on feign
店铺进销存管理系统源码
SlicePlane的Heading角度与Math.atan2(y,x)的对应转换关系
Rxswift -- (1) create a project
二级造价工程师考试还没完?还有资格审核规定!
BioVendor sRAGE Elisa试剂盒化学性质和技术研究
摄像头基础知识
Opencv实现颜色检测
Voltage mode and current mode control of switching power supply
2022年材料员-通用基础(材料员)操作证考试题库及答案
分享|智慧环保-生态文明信息化解决方案(附PDF)
DH parameters of robotics and derivation using MATLAB symbolic operation
[skywalking] learn distributed link tracking skywalking at one go