Dart abstract classes and polymorphism

/ * 
Dart abstract class: Dart abstract class is mainly used to define the standard, subclasses inherit the abstract class can implement the abstract class interface. 


  1, the abstract class defined by the abstract keyword 

  2, Dart abstract method can not be used in the abstract declaration, there are no methods Dart body we call abstract methods. 

  3, if the subclass inherits the abstract classes which have to implement the abstract method 

  4, as if the abstract class interface, then all of the properties and methods defined inside abstract class have to implement. 

  5, an abstract class can not be instantiated, only inheritance can it subclasses 

difference extends abstract class and implements of: 

  1, if you want to reuse the abstract class inside the method, and use abstract methods constrained from class, then we will use extends inheritance abstract class 

  2, if only the abstract class as the standard, then we implemented implements the abstract class 



case: define a class Animal requires its subclasses must contain the eat method 

* / 

abstract  class Animal { 
  eat ();    // abstract method 
  run ( );   // abstract method   
  printInfo () {
     Print ( 'I is an abstract class common method inside' ); 
  } 
} 

class Dogthe extends Animal { 
  @override 
  EAT () { 
     Print ( 'the dog a bone,' ); 
  } 

  @override 
  RUN () { 
    // the TODO: Implement RUN 
    Print ( 'run in the dog' ); 
  }   
} 
class Cat the extends Animal { 
  @override 
  EAT () { 
    // the TODO: Implement EAT 
    Print ( 'kittens eat rats' ); 
  } 

  @override 
  rUN () { 
    // the TODO: Implement rUN 
    Print ( 'run in the cat' ); 
  } 

} 

main ( ) { 

  Dog D = new newDog (); 
  D . EAT (); 
  D . PrintInfo (); 

   Cat C = new new Cat (); 
  C . EAT (); 

  C . PrintInfo (); 


  // Animal A Animal new new = (); // abstract class can not be instantiated directly 

}
/ * 
DATR polymorphism in: 
    allows the assignment of a pointer to a sub-class type parent pointer type, with a function call performed have different effects. 

    Examples of subclass assigned to the reference to the parent class. 
    
    Polymorphism is the parent class defines a method not realize, let him inherit a subclass to achieve, each sub-class has different performance. 

* / 


Abstract  class Animal { 
  EAT ();    // abstract method 
} 

class Dog the extends Animal { 
  @override 
  EAT () { 
     Print ( 'the dog a bone,' ); 
  } 
  RUN () { 
    Print ( 'RUN' ); 
  } 
} 
class Cat the extends Animal { 
  @override 
  EAT () {    
    Print ( 'kittens eat rats' ); 
  } 
  RUN () {
    print('run');
  }
}

main(){

  // Dog d=new Dog();
  // d.eat();
  // d.run();


  // Cat c=new Cat();
  // c.eat();




  Animal d=new Dog();

  d.eat();

 

  Animal c=new Cat();

  c.eat();

 


}

 

Guess you like

Origin www.cnblogs.com/loaderman/p/11026819.html