当前位置:网站首页>Typescript TS basic knowledge and so on
Typescript TS basic knowledge and so on
2022-07-25 23:50:00 【Tangtang 246】
1. Use keywords class Define a class
class Person { }
2. Define instance properties in the class
adopt new The instance object generated by the call can access
class Person{
name:string = ' name '
age:number = 18
}
const per = new Person()
console.log(per.name)
3. static Class properties / Static attribute
adopt 【 Class name . Property name 】 visit , Use keywords static modification , There is no need to create an instance to use , Instance object cannot be read
class Person{
name:string = ' name '
staticage:number = 18
}
const per = new Person()
console.log(per.name, Person.age)
4. readonly Read-only property
class Person{
readonly name:string = ' name '
static readonly age:number = 18
}
5. Define methods
Example method Medium this Represents the object that currently invokes the method
class Person{
say123(){ console.log(123)
static say456(){ console.log(123)
}
const per = new Person()
per.say123()
Person.say456()
6. constructor Constructors
Constructors are created on objects (new call ) Called when the , Function this Represents the currently created instance object
class Person{
name:string
age:number
constructor(name:string, age:number){
this.name = name
this.age = age
}
}
const ming = new Person(' Xiao Ming ', 16)
const fang = new Person(' Xiaofang ', 18)7. extends Inherit
class Subclass extends Parent class { }
After using inheritance , The subclass will have all the methods and properties of the parent class ; By inheritance , We can write code common to multiple classes in a parent class , In this way, you only need to write once to make all subclasses have properties and methods in the parent class at the same time
If you want to add unique attributes or methods to subclasses , Just write it directly . If a method with the same name in the parent class is added to the subclass , Then the subclass method will override the method in the parent class , We call it Method rewriting .
class Animal{
name:string
age:number
constructor(name:string, age:number){
this.name = name
this.age = age
}
sayHi(){
console.log(' The animal barks again !')
}
}
class Dog extends Animal{
run(){
console.log(this.name+' Running ~')
}
}
class Cat extends Animal{
}
const cat = new Cat(' kitten ', 2)
const dog = new Dog(' puppy ', 3)
dog.run()
// cat.run() // Report errors
cat.sayHi()Reference the parent method in the subclass method , use super Keyword indicates the parent class of the current class
class Dog extends Animal {
sayHi(){ super.sayHi() }
}
If you write constructor Constructors , You need to call the constructor of the parent class in the subclass 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 class
With abstract The class starting with the keyword is an abstract class , Is a special class for inheritance , Can't be used to create objects
Abstract methods can be added to abstract classes , Define only the structure , Do not define specific implementation . Abstract methods can only be defined in abstract classes , Subclasses must override abstract methods
abstract class Animal{
name:string
constructor(name:string){
this.name = name
}
abstract sayHi():void
}
class Dog extends Animal{
sayHi(): void { ... }
}
边栏推荐
- Static agent + dynamic agent
- Regular expression (user name form verification / verification of landline number / regular replacement)
- 1223. 掷骰子模拟 范围DP
- Qpprogressbar for QT style (QSS) application
- [code case] blog page design (with complete source code)
- Native JS perfectly realizes deep copy
- ArcGIS cuts TIF images (grid data) according to the vector range, merges shp files in batches, cuts vectors in the region according to the vector range, outputs the geographic coordinate system, conve
- chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
- Bubble sort idea and Implementation
- 利用用户脚本优化 Yandere/Konachan 站点浏览体验
猜你喜欢

浅识 OWASP

Qpprogressbar for QT style (QSS) application

Graph traversal DFS, BFS (code explanation)

Docker 安装 Redis-5.0.12(远程访问)

Read the field status of account in ABAP code (hidden, optional, required)

反射之类加载过程

Matchmaker's words

Leetcode 0135. distribute candy

The process of finding free screen recording software - I didn't expect win10 to come with this function

Optimize the browsing experience of yandere/konachan site with user scripts
随机推荐
指针函数的demo
Same origin strategy and cross domain
面试重点——传输层的TCP协议
initializer_list工具库学习
《数据密集型应用系统设计》 - 应用系统概览
Regular expression (user name form verification / verification of landline number / regular replacement)
C# - readonly 和 const 关键字
Good news under the epidemic
Problem set
utility实用组件学习之swap,move,forward,exchange
LeetCode 0135. 分发糖果
【ManageEngine】ServiceDesk Plus荣获2022安全样板工程数据安全奖
Scroll series
Payment terms in SAP message No. vg202 IDoc e1edk18 have been transferred: check data
[Muduo] package EventLoop and thread
[nodejs] nodejs create a simple server
Grain Academy p98 trample pit e.globalexceptionhandler: null
typescript ts 基础知识之类
S4/HANA MM & SD EDI基于NAST的集成配置(ORDERS, ORDRSP, DESADV, INVOIC)
热部署和热加载有什么区别?