当前位置:网站首页>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')
}
}
边栏推荐
- When a user logs in, there is often a real-time drop-down box. For example, entering an email will @qq com,@163. com,@sohu. com
- Clear app data and get Icon
- SQL series (basic) - Chapter 2 limiting and sorting data
- Talk about my fate with some programming languages
- ProSci LAG3抗体的化学性质和应用说明
- ProSci LAG-3 重组蛋白说明书
- Abnova cyclosporin a monoclonal antibody and its research tools
- Duchefa丨MS培养基含维生素说明书
- 驱动壳美国测试UL 2043 符合要求有哪些?
- matplotlib绘图润色(如何形成高质量的图,例如设如何置字体等)
猜你喜欢

LeetCode_哈希表_困难_149. 直线上最多的点数

Prosci LAG-3 recombinant protein specification

Phpstudy Xiaopi's MySQL Click to start and quickly flash back. It has been solved

Écrire une interface basée sur flask

基于AVFoundation实现视频录制的两种方式

学习机器人无从下手?带你体会当下机器人热门研究方向有哪些

Duchefa丨P1001植物琼脂中英文说明书

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

台风来袭!建筑工地该如何防范台风!

2.<tag-哈希表, 字符串>补充: 剑指 Offer 50. 第一个只出现一次的字符 dbc
随机推荐
leetcode:1139. 最大的以 1 为边界的正方形
Écrire une interface basée sur flask
ProSci LAG-3 重组蛋白说明书
ViewRootImpl和WindowManagerService笔记
Analysis of steam education mode under the integration of five Education
Clion-MinGW编译后的exe文件添加ico图标
Analyze the knowledge transfer and sharing spirit of maker Education
如何让化工企业的ERP库存账目更准确
Chemical properties and application instructions of prosci Lag3 antibody
sql系列(基础)-第二章 限制和排序数据
Is the securities account given by the school of Finance and business safe? Can I open an account?
Learning notes of SAS programming and data mining business case 19
poj 3414 Pots (bfs+线索)
挖财商学院给的证券账户安全吗?可以开户吗?
Comparison table of foreign lead American abbreviations
Simple getting started example of Web Service
Is it safe to open a stock account by mobile phone? My home is relatively remote. Is there a better way to open an account?
Sophomore personal development summary
Enclosed please find. Net Maui's latest learning resources
PHP反序列化+MD5碰撞