TS语法中interface和class的理解

在TS中interface和后端语言如c#中的概念是不一样的,在TS中interface相当于定义了一种类型,是设置自定义类型的方式,区分与基础类型(number、string等),当定义变量时,就可以设置该变量为已经设置的interface类型,如下:

interface IPerson { firstName:string, lastName:string, sayHi: ()=>string }

var customer:IPerson = { firstName:"Tom", lastName:"Hanks", sayHi: ():string =>{return "Hi there"} }

上面customer是个IPerson类型的变量,并进行了初始化;

在TS中class和c#中的概念是相似的,class也支持继承,定义好类后,通过new关键字初始化对象;

另外class也可以实现接口,使用关键字 implements;

 

posted @ 2022-10-20 17:38  路鸣  阅读(1449)  评论(0编辑  收藏  举报