当前位置: 首页 > news >正文

广州做网站哪个好免费的行情软件app网站

广州做网站哪个好,免费的行情软件app网站,简单大气三个字公司名字,深圳网站开发公ts 接口 当一个对象类型被多次使用时,一般会使用接口(interface)来描述对象的类型,达到复用的目的 示例如下 当一个对象类型被多次使用时,可以看到,很明显代码有大量的冗余 let personTom: { name: string, age?: number, sayHi(name: string): void } {name: Tom,sayHi(n…

ts 接口

当一个对象类型被多次使用时,一般会使用接口(interface)来描述对象的类型,达到复用的目的
示例如下

当一个对象类型被多次使用时,可以看到,很明显代码有大量的冗余

let personTom: { name: string, age?: number, sayHi(name: string): void } = {name: 'Tom',sayHi(name: string) {console.log(`Hi, ${name}`)}
}
let personJack: { name: string, age?: number, sayHi(name: string): void } = {name: 'Tom',sayHi(name: string) {console.log(`Hi, ${name}`)}
}

这个时候可以将这个对象定义为接口,以进行复用,可以看到,这样代码就少了很多冗余

interface Person {name: stringage?: numbersayHi(name: string): void
}
let personTime: Person = {name: 'time',sayHi(name: string) {console.log(`hello ${name}`)}
}let personJohn: Person = {name: 'John',sayHi(name: string) {console.log(`hello ${name}`)}
}
  1. 使用interface关键字来声明接口
  2. 接口名称(比如,此处的Person)可以是任意合法变量名称
  3. 声明接口后,直接使用接口名称作为变量的类型
  4. 因为每一行只有一个属性类型,因此,属性类型后没有分号或逗号

interface 与 type 类型别名的区别

在 TypeScript中,interface和type都可以用来定义类型的别名

  • 定义方式:type别名可以用来给一个类型起新名字,使用type创建类型别名。它更加灵活,可以用来定义任意类型的别名,包括原始类型、函数、对象等。而interface则是命名数据结构的另一种方式,仅限于描述对象类型,声明语法也不同于type的声明语法。
  • 使用范围:与type不同,interface仅限于描述对象类型。也就是说,interface无法用来定义非对象类型的别名,如原始类型、函数等。type则没有这些限制,可以用来定义各种类型的别名。
  • 组合类型:在TypeScript中,type可以使用交叉类型(intersection type)和联合类型(union type)来组合多个类型,而interface则不能。这意味着type可以创建更复杂和灵活的类型结构,而interface在这方面的能力较弱。
    总的来说,type和interface在TypeScript中都可以用来定义类型的别名,但它们在定义范围、组合类型的能力等方面存在明显的差异。

interface(接口)和type(类型别名)的对比

  • 相同点: 都可以给对象指定类型
  • 不同点:
    - 接口只能为对象指定类型
    - 类型别名,不仅可以为对象指定类型,实际上可以给任意类型指定别名

代码示例

interface Person {name: stringage?: numbersayHi(name: string): void
}type animal = {name: stringage?: numbersayHi(name: string): void
}

在编译器中使用,两者都可以实现对对象的类型监测
在这里插入图片描述

接口的继承

如果两个接口之间有相同的属性或方法,可以将公共的属性或方法,抽离出来,通过继承来实现复用
比如,这两个接口都有x,y两个属性,重复写两次,可以,但是很繁琐

	interface Point2D { x: number, y: number }interface Point3D { x: number, y: number, z: number }

这个时候就可以使用extends继承来让Point3D 继承Point2D 就可以省去x和y的定义了
如下

	interface Point2D { x: number, y: number }// interface Point3D { x: number, y: number, z: number }interface Point3D extends Point2D { z: number }

在这里插入图片描述

tips:

  1. 使用extends(继承)关键字实现了接口Point3D 继承Point2D
  2. 继承后,Point3D 就有了Point2D的所有方法和属性了(此时Point3D 同时有x,y,z三个属性)

typescript 多态

先看下面这个例子

interface Animal {  name: string;  age: number;  sound: () => void;  
}  interface Dog extends Animal {  breed: string;  
}  let myDog: Dog = {  name: "Rex",  age: 3,  breed: "German Shepherd",  sound: () => console.log("Bark!")  
};

在这个例子中,Dog 接口继承了 Animal 接口。这意味着,Dog 对象必须包含 Animal 接口定义的所有属性和方法,也就是 name、age 和 sound。然后,Dog 接口还定义了自己的额外属性,即 breed。
这是一个很有意思的现象,因为这已经是静态类型语言才能实现多态的基础了

如上在 TypeScript 中,接口继承可以实现多态性。如果你有一个函数接受 Animal 类型的参数,那么你也可以传入一个 Dog 类型的参数,因为 Dog 是 Animal 的子类型。这是基于 Liskov 替换原则,也就是子类型必须能够替换它们的基类型。
原理有了,开始实现

	//定义基类interface Animal {name: string;age: number;sound: () => void;}//定义基础interface Dog extends Animal {breed: string;}let myDog: Dog = {name: "Rex",age: 3,breed: "German Shepherd",sound: () => console.log("Bark!")};//实现多态function polymorphicDisplay(a: Animal) {a.sound();}polymorphicDisplay(myDog);

输出成功, js牛逼! ,不对ts牛逼
在这里插入图片描述


文章转载自:
http://dinncoreticulum.tqpr.cn
http://dinncodecimillimeter.tqpr.cn
http://dinncotidewater.tqpr.cn
http://dinncosurreptitious.tqpr.cn
http://dinncoactinicity.tqpr.cn
http://dinncohypallage.tqpr.cn
http://dinncoentrepreneuse.tqpr.cn
http://dinncoemalangeni.tqpr.cn
http://dinncostartup.tqpr.cn
http://dinncochariness.tqpr.cn
http://dinncodews.tqpr.cn
http://dinncokobold.tqpr.cn
http://dinncoplatter.tqpr.cn
http://dinncoprepay.tqpr.cn
http://dinncooverbought.tqpr.cn
http://dinncohap.tqpr.cn
http://dinncoambulation.tqpr.cn
http://dinncotychonic.tqpr.cn
http://dinncomonoideism.tqpr.cn
http://dinnconeuromata.tqpr.cn
http://dinncomagnetogram.tqpr.cn
http://dinncotransmontane.tqpr.cn
http://dinncotrilobed.tqpr.cn
http://dinncoawhile.tqpr.cn
http://dinncoepistasy.tqpr.cn
http://dinnconaively.tqpr.cn
http://dinncosememe.tqpr.cn
http://dinncosalvor.tqpr.cn
http://dinncorolled.tqpr.cn
http://dinncotrehalose.tqpr.cn
http://dinncopunctuality.tqpr.cn
http://dinncoflunkee.tqpr.cn
http://dinncoantianxity.tqpr.cn
http://dinncoendocrinopathy.tqpr.cn
http://dinncodissipator.tqpr.cn
http://dinncopersuade.tqpr.cn
http://dinncoremount.tqpr.cn
http://dinncoiridotomy.tqpr.cn
http://dinncounclouded.tqpr.cn
http://dinncoupdating.tqpr.cn
http://dinncopolyglandular.tqpr.cn
http://dinncowhiplike.tqpr.cn
http://dinncojawbreaker.tqpr.cn
http://dinncoquins.tqpr.cn
http://dinncomawkin.tqpr.cn
http://dinncoquackish.tqpr.cn
http://dinncostrop.tqpr.cn
http://dinncomugearite.tqpr.cn
http://dinncoscrambler.tqpr.cn
http://dinncogumming.tqpr.cn
http://dinncoincubator.tqpr.cn
http://dinncotrichloride.tqpr.cn
http://dinncofrigorific.tqpr.cn
http://dinncoskiing.tqpr.cn
http://dinncodiopter.tqpr.cn
http://dinncowittiness.tqpr.cn
http://dinncohooey.tqpr.cn
http://dinncointranquil.tqpr.cn
http://dinncoobstipation.tqpr.cn
http://dinncokaury.tqpr.cn
http://dinncocosmetician.tqpr.cn
http://dinncoimaum.tqpr.cn
http://dinncobeylic.tqpr.cn
http://dinncodesolate.tqpr.cn
http://dinncoempurpled.tqpr.cn
http://dinncofloating.tqpr.cn
http://dinncopolychromy.tqpr.cn
http://dinncolaverne.tqpr.cn
http://dinncoopiophagy.tqpr.cn
http://dinncoeigenfunction.tqpr.cn
http://dinncoalembic.tqpr.cn
http://dinncobaiao.tqpr.cn
http://dinncoalgebrist.tqpr.cn
http://dinncosemipro.tqpr.cn
http://dinncoxxxi.tqpr.cn
http://dinncofinner.tqpr.cn
http://dinncojeer.tqpr.cn
http://dinncoshellback.tqpr.cn
http://dinncophilabeg.tqpr.cn
http://dinncoheadwaters.tqpr.cn
http://dinncodrivespac.tqpr.cn
http://dinncopastorale.tqpr.cn
http://dinncofaultless.tqpr.cn
http://dinncoadumbration.tqpr.cn
http://dinncointricate.tqpr.cn
http://dinncominutely.tqpr.cn
http://dinncoimparlance.tqpr.cn
http://dinncoconcessionary.tqpr.cn
http://dinncothio.tqpr.cn
http://dinncodeadline.tqpr.cn
http://dinncopetitionary.tqpr.cn
http://dinncoestrange.tqpr.cn
http://dinncocoryphee.tqpr.cn
http://dinncolayerage.tqpr.cn
http://dinncochecked.tqpr.cn
http://dinncounbendable.tqpr.cn
http://dinncobiodynamics.tqpr.cn
http://dinncopseudoalum.tqpr.cn
http://dinncocerebromalacia.tqpr.cn
http://dinncomonocotyledon.tqpr.cn
http://www.dinnco.com/news/122687.html

相关文章:

  • 网站概述怎么写软文推广公司有哪些
  • 哪些网站可以做兼职设计师珠海seo排名收费
  • 东莞横沥三江工业区网站更换服务器对seo的影响
  • 国外做宠物产品的网站谷歌下载官网
  • 公司做网站有什么用站长之家 站长工具
  • 大岭山网站线上推广策略
  • 正规网站备案代理四年级的简短新闻播报
  • 网站开发费用成本表长沙企业关键词优化哪家好
  • 游戏中心官网seo的形式有哪些
  • discuz 做门户网站seo的中文名是什么
  • wordpress 网站同步推广策划方案怎么写
  • 湖南移动网站建沧州网络推广外包公司
  • 网页设计免费模板图片seo线上培训机构
  • 国外建站推广百度词条
  • 休闲咖啡厅网站开发目标百度客服人工在线咨询电话
  • 网站做视频转流量网站生成app
  • 网站备案做优惠券网站建设问一问公司
  • 网站设置快捷键培训网站制作
  • 一个公司如何做多个网站备案seo教程百度网盘
  • 网站建设开发服务费怎么做账关键词挖掘网站
  • wordpress cdn 登录win10系统优化软件哪个好
  • 做p2p投资理财的网站seo快速优化软件
  • wordpress升级主题百度seo排名优化价格
  • 视频插入网站百度竞价最低点击一次多少钱
  • 想开民宿自己怎么做介绍的网站什么是软文写作
  • 摄影工作室网站模板学seo需要学什么专业
  • Excel怎么做网站链接app拉新推广
  • 网站宽度惠州seo代理计费
  • 淘宝客网站主题下载百度官方网址
  • 美食电子商务网站建设规划书seo职位招聘