当前位置:网站首页>typescript ts 基础知识之类
typescript ts 基础知识之类
2022-07-25 23:37:00 【糖糖246】
1. 使用关键字class定义一个类
class Person { }
2. 定义类中的实例属性
通过new调用产生的实例对象可以访问
class Person{
name:string = '名字'
age:number = 18
}
const per = new Person()
console.log(per.name)
3. static 类属性/静态属性
通过【类名.属性名】访问,使用关键字static修饰,不需要创建实例使用,实例对象读取不到
class Person{
name:string = '名字'
staticage:number = 18
}
const per = new Person()
console.log(per.name, Person.age)
4. readonly 只读属性
class Person{
readonly name:string = '名字'
static readonly age:number = 18
}
5. 定义方法
实例方法中的this表示当前调用方法的对象
class Person{
say123(){ console.log(123)
static say456(){ console.log(123)
}
const per = new Person()
per.say123()
Person.say456()
6. constructor 构造函数
构造函数在对象创建(new调用)时调用,函数中的this表示当前新建的实例对象
class Person{
name:string
age:number
constructor(name:string, age:number){
this.name = name
this.age = age
}
}
const ming = new Person('小明', 16)
const fang = new Person('小芳', 18)7. extends 继承
class 子类 extends 父类 { }
使用继承后,子类将会拥有父类的所有方法和属性;通过继承,我们可以将多个类中共有的代码写在一个父类中,这样只需要写一次即可让所有子类都同时拥有父类中的属性和方法
若想在子类中添加独有的属性或方法,直接写即可。如果在子类中添加了父类中同名方法,则子类方法会覆盖掉父类中的方法,我们称之为方法重写。
class Animal{
name:string
age:number
constructor(name:string, age:number){
this.name = name
this.age = age
}
sayHi(){
console.log('动物再叫!')
}
}
class Dog extends Animal{
run(){
console.log(this.name+'在跑~')
}
}
class Cat extends Animal{
}
const cat = new Cat('小猫', 2)
const dog = new Dog('小狗', 3)
dog.run()
// cat.run() //报错
cat.sayHi()在子类方法中引用父类方法,用super关键字表示当前类的父类
class Dog extends Animal {
sayHi(){ super.sayHi() }
}
如果在子类中写了constructor构造函数,需要在子类构造函数中对父类的构造函数进行调用,super()
class Animal{
name:string
constructor(name:string){ this.name = name }
}
class Dog extends Animal{
age:number
constructor(name:string, age:number){
super(name)
this.age = age
}
}
8. abstract 抽象类
以abstract关键字开头的类是抽象类,是专门用来继承的类,不能用来创建对象
抽象类中可以添加抽象方法,只定义结构,不定义具体实现。抽象方法只能定义在抽象类中,子类必须对抽象方法进行重写
abstract class Animal{
name:string
constructor(name:string){
this.name = name
}
abstract sayHi():void
}
class Dog extends Animal{
sayHi(): void { ... }
}
边栏推荐
- numeric学习之iota,accumulate
- Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions
- [QNX hypervisor 2.2 user manual]9.6 GDB
- 从哪些维度评判代码质量的好坏?如何具备写出高质量代码的能力?
- Serialize operator
- Leetcode 0919. complete binary tree inserter: array representation of complete binary tree
- Inheritance (the child constructor inherits the attributes in the parent constructor)
- Unexpected dubug tricks
- About the foundation of fetch
- Mongodb update operator (modifier)
猜你喜欢

Matchmaker's words

疫情之下的好消息

Why are there many snapshot tables in the BI system?

Leetcode 0919. complete binary tree inserter: array representation of complete binary tree

R语言安装教程 | 图文介绍超详细
![[Muduo] EventLoop event cycle](/img/80/824c7061d58796d454be0c438e257c.png)
[Muduo] EventLoop event cycle

redis-基本数据类型(String/list/Set/Hash/Zset)

Classes and objects (2) (6 default member functions)

WebMvcConfigurationSupport

Idea sets get and set templates to solve the naming problem of boolean type fields
随机推荐
学习探索-3d轮播卡片
initializer_ List tool library learning
Qt风格(QSS)应用之QProgressBar
LeetCode 0919. 完全二叉树插入器:完全二叉树的数组表示
【MUDUO】Thread封装
[Muduo] thread package
Strategy mode_
[JUC] concurrent keyword volatile
在应用中使用 Jetpack 库
LeetCode 0135. 分发糖果
S4/hana ME21N create Po output control message button missing solution (switch EDI output mode brf+ to Nast mode)
Reduce method of array
TS basic data type
This point inside the function / change this point inside the function
From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
About the foundation of fetch
1913. Maximum product difference between two number pairs - no sorting required
LeetCode 0136. 只出现一次的数字:异或
Implementation of date class
Serialize data type