Object-oriented features of the three - Polymorphism

A polymorphic: with a different type of reference Example perform different operations.
Second, the idea of using the polymorphic implementation:
1 parent class write
method of the preparation of the parent class 2 subclass, class overrides.
3 runtime, object type using the parent class, subclass.
NOTE: upcast (subclass is converted into the parent class)
the Pet Dog PET new new = ();
downcast (cast) (converted into the parent subclasses)
Dog Dog = (Dog) PET 
cast is generally used in combination in combination intanceof the syntax: Object intanceof class or interface. Used to determine whether the type of the back of the front. When subclass specific methods generally used, in the test class needs to call this subclass specific method, the need cast.
Three implement two forms of polymorphism
a parent class as a method of using multi-state parameter.
2 using the parent class method returns a value as to achieve polymorphism.
Four realize the benefits of multi-state
polymorphic class code amount can be reduced, can improve scalability and maintainability of the code of
a parent
public the Person class {
   public void Show () {
       System.out.println ( "*** *** I am a human ******* ");
   }
}
two sons class boy
public boys class the extends the Person {
   public void Show () {
       System.out.println (" I'm a boy ");
   }
   
   Public void Play () {
       System.out.println ( "We love playing games");
   }
}
sub-class girl
public Girls class the extends the Person {
     public void Show () {
         System.out.println ( "I am a girl." );
     }
     
     public void Play () {
         System.out.println ( "shopping our love");
     }
}
management class: public class Manager {
     public void showinformation (the Person P) {
         IF (P Boys the instanceof) {
             Boys people = (Boys) P;
             people.play ();
         } the else IF (P Girls the instanceof) {
             Girls people = (Girls) P;
             people.play ();
         }
     }
Note: the parent class implementation of the method as a parameter polymorphism

the Person GetInformation public (int NUM) {
         the Person P1 = null;
         IF (NUM ==. 1) {
             P1 = new new Boys ();
             p1.show ();
         } the else IF (NUM == 2) {
             P1 = new new Girls () ;
             p1.show ();
         }
        
         return P1;
     }
Note: the parent class as the return value of multi-state
  public GetInformation the Person (int NUM) {
         the Person P1 = null;
         IF (NUM ==. 1) {
             P1 = new new Boys ( );
             p1.show ();
         } the else IF (NUM == 2) {
             P1 = new new Girls ();
             p1.show ();
         }
        
         return P1;
     }
test class
 public static void main(String[]args){
        Manager you=new Manager();
        Person p=new Girls();
        you.showinformation( p);        
        Person  ty =you.getInformation(1);
       
        
    }

Guess you like

Origin www.cnblogs.com/itchenguo/p/10974437.html