typescript abstract class

1. 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.
2. abstract abstract methods abstract class only on the inside
3. abstract classes and abstract methods to define standard. Standard: Animal class requires its subclasses must eat method comprising
example:
abstract class Animal{
public name:string;
constructor(name:string){
this.name=name;
}
abstract eat (): any; // abstract method does not comprise specific implementation and must be implemented in the derived class.
run(){
the console.log ( 'other methods may not be implemented')
}
}
// var a = new Animal () / * error * writing /
class Dog extends Animal{
// subclass of abstract class abstract methods inside the abstract class must implement
constructor(name:any){
super(name)
}
eat(){
console.log (this.name + 'eating food')
}
}
var d = new Dog ( 'small flower');
d.eat();
class Cat extends Animal{

// subclass of abstract class abstract methods inside the abstract class must implement
constructor(name:any){
super(name)
}
run(){
}
eat(){
console.log (this.name + 'eat rats')
}
}
var c = new Cat ( 'small cat');
c.eat();

Guess you like

Origin www.cnblogs.com/zhx119/p/12104346.html