当前位置:网站首页>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')
}
}
边栏推荐
- 序列联配Sequence Alignment
- Enclosed please find. Net Maui's latest learning resources
- Comparison table of foreign lead American abbreviations
- Duchefa cytokinin dihydrozeatin (DHZ) instructions
- The development of research tourism practical education helps the development of cultural tourism industry
- 实现浏览页面时校验用户是否已经完成登录的功能
- Norgen AAV提取剂盒说明书(含特色)
- Careercup its 1.8 serial shift includes problems
- Analyze the knowledge transfer and sharing spirit of maker Education
- SQL series (basic) - Chapter 2 limiting and sorting data
猜你喜欢

PHP反序列化+MD5碰撞

Return to blowing marshland -- travel notes of zhailidong, founder of duanzhitang

解析五育融合之下的steam教育模式

Abnova e (diii) (WNV) recombinant protein Chinese and English instructions

【案例】定位的运用-淘宝轮播图

Duchefa MS medium contains vitamin instructions

Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing

Specification of protein quantitative kit for abbkine BCA method

珍爱网微服务底层框架演进从开源组件封装到自研

浅聊我和一些编程语言的缘分
随机推荐
Influence of oscilloscope probe on signal source impedance
CLion配置visual studio(msvc)和JOM多核编译
wpf 获取datagrid 中指定行列的DataGridTemplateColumn中的控件
Duchefa low melting point agarose PPC Chinese and English instructions
2. < tag hash table, string> supplement: Sword finger offer 50 The first character DBC that appears only once
使用WebAssembly在浏览器端操作Excel
ClickHouse 复制粘贴多行sql语句报错
字典树简单入门题(居然是蓝题?)
国外LEAD美国简称对照表
浅聊我和一些编程语言的缘分
Wanglaoji pharmaceutical's public welfare activity of "caring for the most lovely people under the scorching sun" was launched in Nanjing
基于flask写一个接口
CADD course learning (7) -- Simulation of target and small molecule interaction (semi flexible docking autodock)
学习机器人无从下手?带你体会当下机器人热门研究方向有哪些
教你自己训练的pytorch模型转caffe(三)
Web Service简单入门示例
systemd-resolved 开启 debug 日志
ts 之 泛型
Promouvoir le développement de l'industrie culturelle et touristique par la recherche, l'apprentissage et l'enseignement pratique du tourisme
Abnova CD81 monoclonal antibody related parameters and Applications