JAVA base (13) polymorphism

                                                                  Polymorphic:

 Own understanding:

       Subclass inherits the parent class and override the parent class method, call the method is the subclass method overrides the parent class is called, want to subclass-specific method calls, was downcast after the call.

    Polymorphic:

               1. Definition Format polymorphism: the reference variable is the parent class objects to sub-class

                2. Almost all of encapsulation and inheritance of polymorphic prepared

                3. The premise polymorphism is inherited; the child must have a parent or off the interface and implementation classes relationship; otherwise unable to complete the multi-state;

                4. After the polymorphic method call parent class reference variable, calls the method after the child class overrides.

Polymorphic formats:

                          Parent class type variable name = new subclass type ();

                                              FU a=new ZI();

 

Polymorphic transformation called up; parent class type variable name = new subclass type;

                                                    FU a=new ZI();

                                                   Animal cat=new Cat();

 

 

 

 

         Downcast:                              subclass type variable name = (subclass type) parent class type variable name

   When the transition down to add instranceof judge to judge whether the same type

                   Type variable name = subclass (subclass type) parent class type of variable;

 

                                                              FU  an=new ZI();

                                                                 if(an instanceof ZI){ 

                                                                       Day as = (ZI) year;

                                                                  ca. subclass specific method name ();

                                                                         }

 

 

 Instanceof keyword

   instanceof keyword to determine if an object belongs to a data type.

Using the format:  

          boolean b = instanceof Object data type; the result is true or false;

                   Determining if statement is true plus downward turn, can tune the class-specific methods

    

                              Upcast:

      / Small type (subclass) -> Large type (parent) automatically transferred

                             Downcast:

        / Large type (parent) -> small type (subclass) transfected strong

                      Downcast and upcast

. When to use up the transformation :

                      1. When no face subclass type, improve the reusability of code,

                     2. The variables and methods of the parent class will be able to meet the needs of the time now do not need to call the property subclass-specific;

When is downcast :

                     1. When the need methods specific subclass;

       The benefits of the transition down is:

                            Specific subclass may be used a method;

                            Disadvantages:

                             lass Cast Exception error type is not likely to occur when the corresponding downward transition;

                              In this case need to add instranceof determined whether the same type

                  Code demonstrates :

public class Demo01 {

           public static void main(String[] args) {

          Animal an=new Cat();
          //FU    an=new zi
          //Animal与 Cat()有关系   an=Cat();

         // boolean flag=an instanceof Animal;

          boolean flag=an instanceof Dog;

           System.out.println(flag);

      }

}

 

 

 

 

 

 

 

Package guide 

Import com.orcale.demo01.Animal; 

Import com.orcale.demo01.Cat; 

Import com.orcale.demo01.Dog; 

 

public  class demo01 { 

 

    public  static  void main (String [] args) { 

       Animal AN = GET () ; // GET () = (An AN = new new CAT ();) 

       an.eat (); // call to the subclass eat () to 

       IF (the instanceof Cat AN) { // downcast call subclass specific method 

       C Cat = (CAT) AN; 

       c.catMouse (); 

       } 
          Method ( new new Dog ());  // pass Dog class 
          Method ( new new CAT ());// pass Cat class
 
}    

    // This method tells us how to improve code reusability polymorphic 

    public  static  void Method (Animalt AN) { 

       an.equals ( " AA " ); 

    } / * polymorphism: 

     * have dissimilar functions class, upcast can call different subclasses functions * / 

          subclass rewriting function is the parent class // polymorphism method another implementation  

      

 public  static Animal GET () { 

       Cat C = new new Cat ( ); 
        the return C; 

/ * can be written about the way

        return  new new Cat (); * / 

    }

 

Polymorphic Code Analysis:

 

This method can be used to call the same override a superclass of all subclasses Method

method (each sub-class type) in the main method in repeated calls

                      method(new Dog());

                       method(new Cat())

        public static void method(Animalt an){

       an.equals("aa");

    }

If you call subclass-specific methods: downcast

 

    IF // (AN the instanceof Cat) {// call the downward transition methods specific subclass

       Cat c=(Cat)an;

       c.catMouse();

       }

 

/ * Polymorphism in

       * Features member variables: see variable compile-time parent, do not look subclass.

       * If you have a parent class, the compiler success

       * If the parent is not, the compiler fails

       * Runtime: see variable of the parent class

       * Compile and run to the left to see,

       * Left there is success

       * There is no failure

       * Features member methods: see the parent class at compile time, and if so, the compilation is successful,

       * If the parent is not, then fail

       * Look after the subclass overrides the method runtime

       * Summary: compiler, run left to see there is wood there,

                    Look to the right method to run after rewriting                         

Polymorphic Disadvantages:

                     Own summary: polymorphic unique subclass method can not be called

                                          Tone class method requires downcast, the parent can not be adjusted    

           When referring to sub-class the parent class object upward transition occurred, i.e., turned into the handle object of class type parent type. The benefits of the transformation is hidden up a sub-class type, improves the scalability of the code.

But there are drawbacks upward transition, only the contents of the parent class in common, but can not use the subclass-specific features, limited functionality

usage:

l When using the upward transition:

When no face subclass type, by increasing the extensibility, or use the functionality of the parent class corresponding operation can be done, then you can use the upward transition.

l When using downcast

When you want to use a subclass-specific features, you need to use downcast.

L downcast benefits: You can use a subclass-specific features.

l drawbacks: the need to face the specific subclass object; ClassCastException type conversion occurs easily at the transition exception downward. You must be done to determine the type prior to conversion .

 

Guess you like

Origin www.cnblogs.com/layuechuquwan/p/11288198.html