当前位置:网站首页>Introduction to TS, constructor and its this, inheritance, abstract class and interface
Introduction to TS, constructor and its this, inheritance, abstract class and interface
2022-07-05 20:57:00 【qq_ forty-six million three hundred and two thousand two hundre】
Class
class Person{
// No, static Keywords are the attributes and methods of the instance , It can only be accessed and modified through instances 、 call
name: string = 'tm' // Equate to name='tm'
age: number = 18 // Equate to age=18
// Yes readonly Keyword is a read-only property , Do not modify
// readonly age: number = 18
sayHellow() {
console.log('hellow');
}
// Yes static Keyword is a class attribute ( Static attribute ) And class methods ( Static methods ), It can only be accessed and modified through the class name 、 call
static sex: string = ' Woman ' // Equate to static sex=' Woman '
// readonly When decorating class properties , Must be on static Back
// static readonly sex: string = ' Woman '
static sayHellow() {
console.log('static--hellow');
}
}
console.log(Person.sex);// Woman
Person.sayHellow() //static--hellow
const person1 = new Person()
console.log(person1.name, person1.age); //tm 18
person1.sayHellow()//hellow
Constructor and its this
class Dog{
name: string
age: number
// this Always point to the current instance object
constructor(name: string, age: number) {
this.name = name
this.age = age
}
// this Always point to the current call instance object
bark() {
console.log(this.name);
}
}
// Once the use new Create objects , Will call Dog Of constructor Method
const dog1 = new Dog(' Little black ', 3)
const dog2 = new Dog(' The small white ', 6)
dog1.bark() // Little black
dog2.bark() // The small white
Inheritance and abstract classes
//abstract Keyword decorated class is abstract class , Abstract classes are classes that are designed to be inherited , Can't be used to create objects
abstract class Animal{
name: string
age: number
constructor(name: string, age: number) {
this.name = name
this.age = age
}
//abstract The method of keyword modification is abstract method , Abstract methods have no method bodies , Abstract methods can only be defined in abstract classes , Subclasses must override abstract methods
abstract sayHellow():void
}
// If used extends keyword , be Animal Parent class ,Dog Subclass ; It is equivalent to copying the properties and methods of the parent class into the child class
class Dog extends Animal{
// Subclasses can also add methods and properties by themselves
sex: string
constructor(name: string, age: number, sex: string) {
super(name, age)// Call the constructor of the parent class
this.sex = sex
}
sayHellow() {
console.log(' The dog is barking ');
}
run() {
console.log(' The dog is running ');
}
}
class Car extends Animal{
// Override of parent method
sayHellow() {
console.log(' The cat is barking ');
}
}
const dog = new Dog(' Dogs ', 5, ' Woman ')
console.log(dog.sex); // Woman
dog.sayHellow()// The dog is barking
dog.run()// The dog is running
const car = new Car(' Cat and cat ', 2)
car.sayHellow()// The cat is barking
Interface
// All attributes in an interface cannot have actual values , All methods are abstract methods
// Interface is mainly to define a certain standard , Restricted classes must meet this standard
// Interface is ts Peculiar ,js There is no concept of interface in
interface myInter{
name: string
sayHellow():void // The function does not return a value
}
class MyClass implements myInter{
name: string
constructor(name: string) {
this.name = name
}
sayHellow() {
console.log('aaa')
}
}
边栏推荐
- 10000+ 代码库、3000+ 研发人员大型保险集团的研发效能提升实践
- POJ 3414 pots (bfs+ clues)
- 国外LEAD美国简称对照表
- MySQL fully parses json/ arrays
- 五层网络协议
- Which is the best online collaboration product? Microsoft loop, notion, flowus
- 最长摆动序列[贪心练习]
- Duchefa p1001 plant agar Chinese and English instructions
- 启牛2980有没有用?开户安全吗、
- Sophomore personal development summary
猜你喜欢
Abnova total RNA Purification Kit for cultured cells Chinese and English instructions
Abnova maxpab mouse derived polyclonal antibody solution
显示器要申请BS 476-7 怎么送样?跟显示屏一样吗??
基於flask寫一個接口
Interpreting the daily application functions of cooperative robots
Abnova丨血液总核酸纯化试剂盒预装相关说明书
Which is the best online collaboration product? Microsoft loop, notion, flowus
MySQL fully parses json/ arrays
Duchefa low melting point agarose PPC Chinese and English instructions
Duchefa MS medium contains vitamin instructions
随机推荐
《SAS编程和数据挖掘商业案例》学习笔记# 19
Talk about my fate with some programming languages
php中explode函数存在的陷阱
shell编程100例
Sophomore personal development summary
How to open an account online for futures? Is it safe?
中国的软件公司为什么做不出产品?00后抛弃互联网;B站开源的高性能API网关组件|码农周刊VIP会员专属邮件周报 Vol.097
ts 之 泛型
SYSTEMd resolved enable debug log
基於flask寫一個接口
Web Service简单入门示例
Cutting edge technology for cultivating robot education creativity
最长摆动序列[贪心练习]
【案例】定位的运用-淘宝轮播图
Comparison table of foreign lead American abbreviations
挖财商学院给的证券账户安全吗?可以开户吗?
【案例】元素的显示与隐藏的运用--元素遮罩
Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved
PHP deserialization +md5 collision
大二下个人发展小结