Notes: TypeScript implements object-oriented

Object-oriented

  • Define a class
class 类名{
    
    
	名称:数据类型;
	constructor(参数:数据类型){
    
     } // 构造函数
	方法名():返回值类型{
    
     }
}

Omit the public keyword
, trigger when the constructor is instantiated

  • Attribute modifier
用来修饰类里面的属性和方法
public
	公有  类里面,类外面,子类,都可以访问
		
protected
	保护  类里面,子类,可以访问
		
provite
	私有  只有在 类里面 可以访问
		
  • The inherited method
    super is to initialize the constructor of the parent class, this is the same as es2015
class 子类名 extends 父类名{
    
    
	constructor(参数名a:数据类型){
    
    
		super(参数名a)}
}
  • implements keyword
class C implements A{
    
    
	//。。。
}

The implements keyword treats class A as an interface, which means that class C must implement all methods defined in class A, regardless of whether these methods are implemented in class A by default. At the same time, there is no need to define the super method in class C.

  • Static properties

     在属性名前加一个 static 关键字
    
调用方法
	父类名.属性名
  • Static method static
    add a static keyword before the method is a static method,
    static method can call static properties
class 类名{
    
    
	static 静态方法名(){
    
     }
}

调用:  父类.方法名
  • Polymorphism (belongs to inheritance)

The parent class defines a method not to implement it, and let the subclasses that inherit it implement it. Each subclass has a different performance

父类
class 父类名{
    
    
	属性名a:数据类型;
	constructor(参数a:数据类型){
    
     }
	方法名a(){
    
     }
}
子类
class 父类名 extends 子类{
    
    
	constructor(参数a:数据类型){
    
     
	super(属性名a)
	}
	父类方法名a(){
    
     内容}
}
  • abstract
  1. Use the abstract keyword to define abstract classes and abstract methods. The abstract methods in abstract classes do not contain concrete implementations and must be implemented in derived classes

  2. Abstract methods can only be placed in abstract classes

  3. Abstract classes and abstract methods are used to define standards. The parent class requires that its subclasses must contain a method of the parent class

  4. Abstract classes mainly provide base classes for subclasses and cannot be instantiated directly

抽象类
abstract class 父类名(){
    
    }
抽象方法
abstract class 父类名(){
    
    
	abstract 抽象方法名():返回值类型;
}

Guess you like

Origin blog.csdn.net/m0_47883103/article/details/108268693