The method of the fourth static stresses static properties typescript ---- typescript class polymorphism and abstract classes

Static methods and static properties

static data member for the keyword (properties and methods) defined the class as static, static members directly through the class name calling.

class StaticMem {  
    static NUM: Number; 
   
   static DISP (): void { 
      the console.log ( " NUM is " + StaticMem.num) 
   } 
} 
 
StaticMem.num = 12 is      // initialize static variable 
StaticMem.disp ()        // call the static method
= StaticMem.num 12      // initialize static variables 
StaticMem.disp () // call the static method

Static methods may be to call out directly in the class, no new

The polymorphic typescript

The parent class defines a method not realize, let him inherit a subclass to achieve, each sub-class has different performance

Polymorphic belong to inherit

Example:

 

class Animal { 

  name: String ; 
  construtor (name: String ) {
    the this .name = name; 
 } 
 EAT () { 
   // specifically what to eat? His son inherited class to implement 
   console.log ( " eat method " ) 
 } 

} 


// Dog class inherits the Animal 
class Dog the extends Animal { 

  construtor (name: String ) { 
   Super (name); 
 } 
 EAT () { 
   return  the this . + name " eat food " ; 
 } 


// Cat class inherits Animal 
class Cat extends Animal{

  construtor(name: string){
   super(name);
 }
 eat(){
   return this.name+"";
 }

 

typescript abstract class

 

It is to provide the class inherits the base class can not be instantiated directly.

With abstract keyword and abstract methods defined in an abstract class, an abstract method of the abstract class does not contain specific implementation and must be implemented in the derived class.

abstract abstract methods abstract class only on the inside

Abstract classes and abstract methods to define standard, standard: Animal class requires its subclasses must eat method comprising

abstract this Animal{

abstract eat():any;

}

class Dog extends Animal{
  //抽象类的子类必须实现抽象类里面的抽象方法
  constructor(name:string){
    super(name)
  }
   eat(){
     console.log("吃粮食")
   }
}

这节很简单,和java很像。

 

 

 

Guess you like

Origin www.cnblogs.com/mqflive81/p/11488929.html