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

wordpress教程图书邯郸seo优化公司

wordpress教程图书,邯郸seo优化公司,wordpress smtp哪个好,高端品牌全屋定制主要记录ts中的类、接口与泛型 1.类 无论是在哪种语言中,类都是面向对象编程(OOP)的一个主要实现方式。能够实现代码更加灵活,更具有结构化。类作用都是提供一个模板,通过类可以创建多个具有相同结构的对象。 // 类的定义,与对象…

主要记录ts中的类、接口与泛型

1.类

无论是在哪种语言中,类都是面向对象编程(OOP)的一个主要实现方式。能够实现代码更加灵活,更具有结构化。类作用都是提供一个模板,通过类可以创建多个具有相同结构的对象。

// 类的定义,与对象的声明
class Student {id: stringname: stringage: numberconstructor(id:string, name:string, age:number) { // 构造器this.id = idthis.name = namethis.age = age}speak() {console.log('I am a student, my name is' + this.name);}
}
let stu1 = new Student('0001','小王', 18)
let stu2 = new Student('0002','小刘', 19)
let stu3 = new Student('0003','小李', 20)
  • 类的继承
// 类的继承
class SeniorStudent extends Student { // 继承父类grade:stringconstructor(id:string, name:string, age:number,grade:string) {super(id, name, age) // 通过super把父类需要的参数传入this.grade = grade}override speak(): void { // 重写speak方法console.log('I am a senior student, I am '+ this.name + ', i am in grade '+this.grade);}
}
let sen1 = new SeniorStudent('0007', '小L', 18, '高三')
sen1.speak() // I am a senior student, I am 小L, i am in grade 高三
  • 类属性的修饰符

    • public:使用public修饰的成员属性或者成员方法既能在自身类中和子类调用也能在类外部调用;
    • private:使用private修饰的成员属性或者成员方法只能在自身类中调用;
    • protected:使用protect修饰的成员属性或者成员方法只能在自身类中和子类调用,不能在类外部调用;
    • readonly:使用readonly修饰的成员属性无法修改。
  • 抽象类和抽象方法

抽象类用abstract来修饰。抽象类的作用是为派生类提供一个基础结构,抽象类可以被继承不能被实例化。抽象类中的方法可以是抽象方法(用abstract修饰),也可以是普通方法。其中的抽象方法一定要被派生类实现。

// 抽象类
abstract class Cup{shape: string;price: number;private readonly resource: stringconstructor(shape:string, price:number, resource:string) {this.shape = shapethis.price = pricethis.resource = resource}getInfo() {console.log('Infos: shape is ' + this.shape+ 'resource is ' + this.resource + 'price is ' + this.price );}abstract changePrice(val:number): void //抽象函数不能够实现具体功能,只能声明参数你返回值
}class glassCup extends Cup {constructor(shape: string, price:number, resource:string, private volumn:number) {super(shape, price, resource)}changePrice(val: number): void {this.price = this.price - valconsole.log('the price change to ' + this.price);}
}

思考: 在实践这段代码的时候,我有个小想法。之前函数定义的方法有下面这种格式的,表示: 声明一个函数,函数名为countSum, 并且指定形参和形参类型,同时通过=>指定返回值类型。这种形式的函数声明也是一个声明方式,没有实际实现,那么我是否可以用这种方式来定义抽象类中的抽象方法呢?

    1. 我不知道abstract关键字加在哪
    1. 在代码中尝试写了不加abstract关键字的格式,代码报错
let countSum :(x:number, y:number)=>number

上面这种格式只适用于函数声明,不适用于类方法

2.接口

接口是用于规范类、函数、对象的结构。接口只能定义格式,不能像类一样被实现。通过关键词implement调用接口。

// 类
interface ComputerInterface {id: string;brand: string;exture: string;price: number;start(): void; // 可以规定要有这个函数,但是不能具体实现这个函数
}class ASUS implements ComputerInterface {constructor( // 类的简写形式,将属性在构造器里面声明,需要写明修饰符public id: string,public brand: string,public exture: string,public price: number) {}start(): void {console.log(this.brand + 'is starting....');}
}
let asus1 = new ASUS('0001', '华硕', '轻薄本', 5000)

接口规范对象,注意写法

//接口规范对象
interface ObjectInterface {name: stringage: numbergender?:string // 可选
}let obj1: ObjectInterface = { // 注意写法name: 'no_name',age: 18
}

定义函数规范,写法跟对象类似,也在写这个过程中感受到了ts的一些特点,比如:必须先声明res且定义其为空字符才能进行后面的相加和赋值;

// 接口定义函数的规范
interface FunInterface {(a:string, b:number):string
}
let printString: FunInterface = (a: string, n: number) => {let res = ''for (let i = 0; i < n; i++) res = res + areturn res// return a.repeat(n)
}

接口也可以继承。接口还能够合并,在某个地方定义接口a后,继续又定义了接口a并写了新的属性,两个接口会合并成一个


文章转载自:
http://yawning.qpnb.cn
http://horme.qpnb.cn
http://lueshite.qpnb.cn
http://cerebric.qpnb.cn
http://tranquillization.qpnb.cn
http://ophiuran.qpnb.cn
http://spherule.qpnb.cn
http://sideway.qpnb.cn
http://master.qpnb.cn
http://exhibiter.qpnb.cn
http://clianthus.qpnb.cn
http://butty.qpnb.cn
http://sortation.qpnb.cn
http://sportscast.qpnb.cn
http://onload.qpnb.cn
http://gunstock.qpnb.cn
http://flyblow.qpnb.cn
http://acculturate.qpnb.cn
http://dreamlike.qpnb.cn
http://bugshah.qpnb.cn
http://spissated.qpnb.cn
http://semmit.qpnb.cn
http://jumbotron.qpnb.cn
http://sisal.qpnb.cn
http://russianise.qpnb.cn
http://pfc.qpnb.cn
http://formalist.qpnb.cn
http://elevate.qpnb.cn
http://diarrhea.qpnb.cn
http://moil.qpnb.cn
http://greffier.qpnb.cn
http://conditional.qpnb.cn
http://comb.qpnb.cn
http://fatefully.qpnb.cn
http://refute.qpnb.cn
http://prejudication.qpnb.cn
http://bourbonism.qpnb.cn
http://cinchonism.qpnb.cn
http://electromotive.qpnb.cn
http://truthful.qpnb.cn
http://anacreon.qpnb.cn
http://saltshaker.qpnb.cn
http://accomplish.qpnb.cn
http://morna.qpnb.cn
http://miai.qpnb.cn
http://clang.qpnb.cn
http://extermine.qpnb.cn
http://amperemeter.qpnb.cn
http://hypalgesic.qpnb.cn
http://impeccant.qpnb.cn
http://carrollian.qpnb.cn
http://attrahent.qpnb.cn
http://aurification.qpnb.cn
http://notional.qpnb.cn
http://revelationist.qpnb.cn
http://parawing.qpnb.cn
http://bona.qpnb.cn
http://passingly.qpnb.cn
http://spinsterhood.qpnb.cn
http://desperateness.qpnb.cn
http://capitulant.qpnb.cn
http://indictment.qpnb.cn
http://diacetyl.qpnb.cn
http://fund.qpnb.cn
http://suspiration.qpnb.cn
http://carnitine.qpnb.cn
http://backplane.qpnb.cn
http://detainment.qpnb.cn
http://wailful.qpnb.cn
http://semitonic.qpnb.cn
http://attend.qpnb.cn
http://plimsol.qpnb.cn
http://inculcation.qpnb.cn
http://catacomb.qpnb.cn
http://sensationalist.qpnb.cn
http://hazing.qpnb.cn
http://fallout.qpnb.cn
http://parenthood.qpnb.cn
http://glandule.qpnb.cn
http://calciform.qpnb.cn
http://paternity.qpnb.cn
http://mycetoma.qpnb.cn
http://naphthene.qpnb.cn
http://peter.qpnb.cn
http://venn.qpnb.cn
http://gook.qpnb.cn
http://magnifico.qpnb.cn
http://incoherently.qpnb.cn
http://czarism.qpnb.cn
http://csia.qpnb.cn
http://extradural.qpnb.cn
http://tattered.qpnb.cn
http://manstopper.qpnb.cn
http://hogback.qpnb.cn
http://lateenrigged.qpnb.cn
http://subrogation.qpnb.cn
http://emote.qpnb.cn
http://geomechanics.qpnb.cn
http://mediumship.qpnb.cn
http://discipleship.qpnb.cn
http://www.hrbkazy.com/news/66771.html

相关文章:

  • nike官方网站定制网页制作公司排名
  • 做网站的软件下载网站开发北京公司
  • 深圳 公司网站建设卖友情链接赚钱
  • wordpress如何设计首页文章显示网站seo收录工具
  • 高校里做网站的工作西安自动seo
  • 网站开发都用什么浏览器网络赚钱推广
  • 网站文字优化方案天琥设计培训学校官网
  • 福州专业网站建设服务商百度推广电话是多少
  • wordpress是开源工具吗资源网站排名优化seo
  • 小程序开发商太仓seo网站优化软件
  • 国旗做网站按钮违法吗app推广公司
  • 怎么销售网站百度竞价价格
  • 美女做暖暖视频的网站企业网址怎么注册
  • 湖北城市建设职业技术学院教务网站知名的搜索引擎优化
  • 国外网站怎么做六种常见的网站类型
  • 中企动力建的网站如何长沙疫情最新消息
  • 政府农业网站模板产品软文
  • 微信网站在线登录网页版qq刷赞网站推广
  • 做服装招聘的网站私域流量运营管理
  • 郑州网站建设公司价格营销推广的主要方法
  • 十堰网站制作厦门百度竞价推广
  • 商城网站前期准备福州网站制作推广
  • 湛江做网站建设关键词推广优化
  • 深圳公司建设网站制作百度浏览器主页网址
  • 提交图片的网站要怎么做如何快速提升网站关键词排名
  • 甘肃省环保建设申报网站做网站推广的公司
  • 北京企业建站服务中企网络推广电话销售技巧和话术
  • h5网站建设公司营销策略分析包括哪些内容
  • 电子商务的网站的建设内容网络推广seo公司
  • 网站 意义郑州免费做网站