当前位置:网站首页>TypeScript 类的使用
TypeScript 类的使用
2022-07-16 11:28:00 【要成为光的女人】
面向对象的三大特性:继承、多态、
一、类的定义最基本方式
1.定一个Person 类
2.在这个类里面定义属性或者方法
3.在new 创建一个对象
4.在创建对象的时候,会执行构造器
5.通过this.name 拿到 Person 里面的那个name,然后把constrector 里面的name 赋值给this.name
/**
1.定一个Person 类
2.在这个类里面定义属性或者方法
3.在new 创建一个对象
4.在创建对象的时候,会执行构造器
5.通过this.name 拿到 Person 里面的那个name,然后把constrector 里面的name 赋值给this.name
*/
class Person{
name:string;
age:number;
constructor(name:string,age:number){
this.name=name
this.age=age
}
eat(){
console.log(this.name+"eating")
}
}
const p=new Person("jiang",18)
console.log(p.name)
console.log(p.age)二、类的继承1: 子类继承父类
//1.因为里面有很多一样的,所以我们可以定义一个继承类,把共同拥有的方法放到这个里面来
class Person{
name:string=""
age:number=0
eating(){
console.log("eating")
}
}
//2.假如这里来了一个学生,希望这个里面可以有姓名年龄,可以使用extends 继承 这个Person
class Student extends Person{
sno:number=0
}
class Teacher extends Person{
title:string=""
}
//3.创建一个对像
const stu =new Student()
//4.
stu.name="jiang"
stu.age=18
二、类的继承2: 子类继承父类(可通过super 关键字调用父类构造器)
注意:此案例是在父类子类都没有初值值的情况下的
1.将所有的初始值删掉
2.在创建学生的之后用contructor 做一个初始化
3.在contructor 里面传入对应的参数
//1.因为里面有很多一样的,所以我们可以定义一个继承类,把共同拥有的方法放到这个里面来
class Person{
name:string
age:number
constructor(name:string,age:number){
this.name=this.name
this.age=age
}
eating(){
console.log("eating")
}
}
//2.假如这里来了一个学生,希望这个里面可以有姓名年龄,可以使用extends 继承 这个Person
class Student extends Person{
sno:number
constructor(name:string,age:number,sno:number){
//super 可以调用父类构造器,把name和age 传进来
super(name,age)
this.name=name
this.age=age
this.sno==sno
}
eating(){
super.eating(). //通过super调用父类里面的代码
}
}
//3.创建一个对像
const stu =new Student("jiang",18,122)
//4.
stu.name="jiang"
stu.age=18三、类的多态
案例1:例如:
- 1.这里定义一个Animal 它们都会会跑
- 2.这里也可以有很多的动物,例如狗,他会继承制Animal
//父类 class Animal{ actions(){} } //子类1: class dog extends Animal{ actions(){ console.log("actions runing") } } //子类2: class fish extends Animal{ actions(){ console.log("actions swmining") } } //子类3: class brird extends Animal{ actions(){ console.log("actions fly") } } //多态的目的是为了写出更家具通用性的代码 function makeActions(animals:Animal[]){ animals.forEach(animals=>{ animals.actions() }) } makeActions([new dog(),new fish()]) //现在是这个狗对象 和鱼的行为
边栏推荐
- 国际NFT交易所排行榜前10名
- View for recording a temperature curve
- [quick application] quick application user agreement and privacy policy content can jump many times. Click back and fail to return to the previous page. How to deal with it?
- 基于 CRON 库扩展的分布式 Crontab 的实现
- Chinese translation of zagayevsky's "try to praise this incomplete world"
- Huawei cloud x Black Lake is a system to solve all kinds of production difficulties in the manufacturing industry!
- 中科蓝讯上市首日跌30%:黄志强60岁创业 公司市值77亿
- How should both parties bear the large amount of child support after divorce
- LeetCode SQL 1669. 176. 第二高的薪水
- Common body and enumeration type
猜你喜欢

2022年全球职业教育行业发展报告

1.4 流程控制语句

Njupt "Xin'an numeral base" Chapter 11 introduction to problem solving

js base64转图片

2022年全球職業教育行業發展報告

【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决

What does software testing need to learn? What skills do test engineers with an annual salary of 30w+ need to master?

Alipay sandbox tests mobile website payment, prompting that the merchant's cooperation agreement has expired and cannot continue to be used

MYSQL的主主/主从复制/xtrabackup/binlog恢复数据库以及使用ansible的常见模块
![[learning record] temporarily cancel the control of tpro (simple)](/img/87/7ebc3caa83de11813bf8a1702ac88e.png)
[learning record] temporarily cancel the control of tpro (simple)
随机推荐
Common body and enumeration type
电脑PC与S7-200SMART PLC不在同一网段,如何建立通信连接?
1.5.2 array
浅析websocket劫持
配置typora+PicGo+Aliyun OSS服务
Health prevention guide 2: healthy diet
1.4 流程控制语句
【AGC】增长服务3-App Linking示例
From physics to AI and war database, the career choice of post-95 programmers
[take you to learn UVM by hand] ~ record all errors encountered
873. Length of the longest Fibonacci subsequence
从物理转 AI 、战数据库,95后程序员的职业选择
华为应用强制更新中,偶现点击“退出应用”退不出应用
调和级数是发散的
How to use pycharm to make a simple Snake game
架构系列之标准Web系统的架构分层
【华为联机对战】下载运行华为官方Unity示例代码,提示鉴权失败并返回错误码100114
基于 CRON 库扩展的分布式 Crontab 的实现
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
关于TCP/IP协议漏洞的安全措施