当前位置:网站首页>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关键字)
边栏推荐
- 二级造价工程师证书含金量到底有多高?看这些就知道了
- Voltage mode and current mode control of switching power supply
- Opencv实现目标检测
- Organize the online cake mall project
- What are functions in C language? What is the difference between functions in programming and functions in mathematics? Understanding functions in programming languages
- Simple usage of GSAP
- Pcr/qpcr research: lumiprobe dsgreen is used for real-time PCR
- The latest examination questions and answers for the eight members (standard members) of Liaoning architecture in 2022
- C语言中函数是什么?编程中的函数与数学中的函数区别?理解编程语言中的函数
- Sorting out some topics of modern exchange principle MOOC
猜你喜欢

How to do a good job of gateway high availability protection in the big promotion scenario

吴恩达深度学习测验题:deeplearning.ai-week1-quiz

How does the power outlet transmit electricity? Simple problems that have plagued my little friend for so many years

Latest Windows version 5.0.14 of redis

How high is the gold content of grade II cost engineer certificate? Just look at this
![[leetcode] 12. Integer to Roman numeral](/img/3e/815f24a85a3333ce924acee1856f62.png)
[leetcode] 12. Integer to Roman numeral

Learning Tai Chi Maker - mqtt Chapter 2 (V) heartbeat mechanism

IP datagram sending and forwarding process

2022 low voltage electrician examination questions and answers

MySQL export database dictionary to excel file
随机推荐
2022 safety officer-b certificate examination question bank and answers
【JVM】——JVM中内存划分
BioVendor sRAGE抗体解决方案
吴恩达深度学习测验题:deeplearning.ai-week1-quiz
JS 文本框失去焦点修改全半角文字和符号
电源插座是如何传输电的?困扰小伙伴这么多年的简单问题
SlicePlane的Heading角度与Math.atan2(y,x)的对应转换关系
Realizing color detection with OpenCV
BioVendor sRAGE蛋白解决方案
深度强化学习笔记
高通平台 Camera 之 MCLK 配置
Prove that there are infinite primes / primes
MCLK configuration of Qualcomm platform camera
Informatics Orsay all in one 1360: strange lift
Operation of simulated examination platform of G3 boiler water treatment recurrent training question bank in 2022
Cgo+gsoap+onvif learning summary: 8. Summary of arm platform cross compilation operation and common problems
Learning Tai Chi Maker - mqtt Chapter 2 (V) heartbeat mechanism
程序员坐牢了,会被安排去写代码吗?
Deeplearning ai-week1-quiz
通过例子学习Rust