当前位置:网站首页>TS study notes class
TS study notes class
2022-07-27 18:41:00 【V_ AYA_ V】
class
Class inheritance
When there is constructor When we use constructors , Be sure to call super()-> Execute the constructor of the base class . Use... In derived classes this Be sure to use super()
class Animal {
name: string;
constructor(theName: string) {
this.name = theName;
}
move(name: string) {
console.log(`${
name} say : I can move `);
}
}
class Dog extends Animal {
constructor(name: string) {
super(name);
}
bark() {
console.log(`${
this.name} I can scream wildly `);
}
move() {
// The subclass's method will override the parent's method
console.log(`${
this.name} Say I can move by myself `);
super.move(this.name); // Calling the base class move Method
}
}
let dog = new Dog(' tearful ');
dog.bark();
dog.move();
public 、 Private and protected modifiers
- public( public ) -> The default value is js It can be written in Chinese, but not in Chinese
- private( private )
- protected( You can access... In a derived class )
- readonly( Read-only property )
More with private or protected Members of the type when , It's different . If one of the types contains a private member , So only if there is one of the other types private member , And they all come from the same declaration , Just think these two types are compatible . about protected Members also use this rule .
class Animal {
private name: string;
constructor(theName: string) {
this.name = theName; }
}
class Rhino extends Animal {
constructor() {
super("Rhino"); }
}
class Employee {
private name: string;
constructor(theName: string) {
this.name = theName; }
}
let animal = new Animal("Goat");
let rhino = new Rhino();
let employee = new Employee("Bob");
animal = rhino;
animal = employee; // error : Animal And Employee Are not compatible .
protected Member types can only be accessed and cannot be used , Constructors can also be marked as protected. This means that this class cannot be instantiated outside the class that contains it , But can be inherited .
Read only properties can only be used in constructor Function .
Static attribute
Static attributes exist on the class itself, not on instances of the class , You can use the class name . Property to access .
class Grid {
static origin = {
x: 0, y: 0};
calculateDistanceFromOrigin(point: {
x: number; y: number;}) {
let xDist = (point.x - Grid.origin.x);
let yDist = (point.y - Grid.origin.y);
return Math.sqrt(xDist * xDist + yDist * yDist) / this.scale;
}
constructor (public scale: number) {
}
}
let grid1 = new Grid(1.0); // 1x scale
let grid2 = new Grid(5.0); // 5x scale
console.log(grid1.calculateDistanceFromOrigin({
x: 10, y: 10}));
console.log(grid2.calculateDistanceFromOrigin({
x: 10, y: 10}));
abstract class
Abstract classes are used as base classes for other derived classes . They are generally not instantiated directly . Unlike interfaces , Abstract classes can contain implementation details of members . Use abstract Keyword definition . Abstract methods in abstract classes do not contain concrete implementations and must be implemented in derived classes
abstract class Department {
constructor(public name: string) {
}
printName(): void {
console.log('Department name: ' + this.name);
}
abstract printMeeting(): void; // Must be implemented in a derived class
}
class AccountingDepartment extends Department {
constructor() {
super('Accounting and Auditing'); // Must be called in the constructor of a derived class super()
}
printMeeting(): void {
console.log('The Accounting Department meets each Monday at 10am.');
}
generateReports(): void {
console.log('Generating accounting reports...');
}
}
let department: Department; // Allows you to create a reference to an abstract type
department = new Department(); // error : Cannot create an instance of an abstract class
department = new AccountingDepartment(); // Allows instantiation and assignment of an abstract subclass
department.printName();
department.printMeeting();
department.generateReports(); // error : Method does not exist in the declared abstract class
Use a class as an interface
Class definitions create two things : Class and a constructor . Because classes can create types , So you can use classes where interfaces are allowed .
class Point {
x: number;
y: number;
}
interface Point3d extends Point {
z: number;
}
let point3d: Point3d = {
x: 1, y: 2, z: 3 };
边栏推荐
- Uniapp has no effect on the page selector on the app side
- 2021.7.31笔记 视图
- Part of speech list of common words
- JPA connection database password field blob
- I was forced to optimize the API gateway query interface
- @Considerations for query of convert annotation in JPA
- XML学习 Day1 : xml / Jsoup解析器 / selector选择器 /Xpath选择器
- [MIT 6.S081] Lec 3: OS organization and system calls 笔记
- Common commands of database 2
- 2021.8.1笔记 数据库设计
猜你喜欢
![[MIT 6.S081] Lab 9: file system](/img/f5/ea30b1fe5b6d73c86f2509c690ca20.png)
[MIT 6.S081] Lab 9: file system

Chained storage structure of dynamic linked list 3 queue (linkedqueue Implementation)

Deep learning: stgcn learning notes

2021.8.1笔记 数据库设计
![[MIT 6.S081] Lec 1: Introduction and examples 笔记](/img/5d/2fc4bde8eebbb22605d314b5292e05.png)
[MIT 6.S081] Lec 1: Introduction and examples 笔记

How to realize the full-text content retrieval of word, PDF and txt files?

2021.7.28 notes

Deep learning: a survey of behavior recognition

Deep learning - paper reading: action structural graph convolution network as-gcn
![[mit 6.s081] LEC 3: OS organization and system calls notes](/img/34/073d00245eb39844bbe1740f65fe07.png)
[mit 6.s081] LEC 3: OS organization and system calls notes
随机推荐
[MIT 6.S081] Lab 10: mmap
MySQL学习 Day3 多表查询 / 事务 / DCL
微信小程序微信支付概述
Deep learning: gat
JDBC learning day1:jdbc
2021.8.9 note request
机器学习——SVM训练集只有一类标签数据而引发的错误
解决Jsp级联问题
MySQL four locks
Deep learning - VIDEO behavior recognition: paper reading - two stream revolutionary networks for action recognition in videos
Complete set of machine learning classification task effect evaluation indicators (including ROC and AUC)
MySQL learning day3 multi table query / transaction / DCL
阿里架构师耗时280个小时整理的1015页分布式全栈小册,轻松入手分布式系统
XML learning Day1: XML / jsup parser / selector /xpath selector
Linked list storage structure of dynamic linked list 2 stack (linkedstack Implementation)
Alibaba architects spent 280 hours sorting out 1015 pages of distributed full stack pamphlets to easily start the distributed system
[MIT 6.S081] Lab 4: traps
Modify placeholder style in input
[MIT 6.S081] Lab 3: page tables
知识图谱 — pyhanlp实现命名体识别(附命名体识别代码)