Polymorphic, downcast and upcast

A polymorphism:

  It is the object of. extends or implements is a prerequisite of polymorphism.

  Manager class inherits from the Employee class. Xiao Ming is a manager object that both managers form, but also employees form. An object has a variety of forms, which is the subject of polymorphism.

  Polymorphism format: parent class object reference to point to a child

    Format: object name parent name = new subclass name ();

    Or: interface name object name = new implementation class name ();

  It can be understood: as a sub-class is the parent class to look at (the employee as a manager is to look at the cat has been treated as animals to look at). The object can only reference to the parent class-specific methods.

Second, the multi-state access member variables of two ways:

  1. Access directly through the object name member variables: Look Who left of the equal sign is a priority with anyone, not then look up. (See left compile, run also see to the left)

Fu obj = new Zi (); 
System.out.println (obj.num);

  2. Indirect access by members of the member variables method: Look who belongs to which priority is who, not then look up. (Look at the subclass of the right side there is no way to find it is not up)

Third, members of the multi-state access methods:

  Regular access member methods: Who is the new look, the priority is with who, then there is no looking upward (see left compile, run to see on the right).

  Look at the left side of the parent class has no method, no not by the compiler error, but running is to look at the right side of the subclass method, it does not look up. Only for members method, does not apply to member variables.

Fourth, the upward transition and the downward transition

  In Java, object variable is polymorphic.

Employee e;
e = new Employee(...);
e = new Manager(...);//Manager继承Employee

  Here Employee class Employee variable can refer to any class and subclass of an object class Employee.

In the code "JAVA core technology Volume" P152 in

Manager boss = new Manager(...);
Employee[] staff = new Employee[3];
staff[0] = boss;

Employee boss = new Employee(...);
Manager[] staff = new Manager[3];
staff[0] = boss;//错误!

  Manager is a boss class object class Employee staff arrays are objects, boss and staff can point to the same object and the compiler staff [0] as Employee object. If the following code will complain, because not all employees are managers (int analog double conversion may lose precision).

  Upward transition must be safe, from small range of steering a wide range (from small managers range of steering a wide range of employees, from small range of cat turned to a wide range of animals, similar to the small range of steering a wide range of double int no loss of accuracy) .
Up transformation of objects is actually multi-state writing:

// format: name of the parent class object name = new subclass name ();
 // the right to create a subclass object, look at it as a parent to use 
the Employee Manager =   new new   the Employee ()
 // create a manager object to to look at it as an employee

  Downcast [reduction] is a movement, but to ensure that when the object is created in the beginning, is the manager, down to the transition from employee to manager. (Determined with the foregoing is not back keyword objects instanceof type)

C = String (String) Staff [. 1 ];
 // gives compiler error because String is not a subclass of Employee

 

  • Can only be cast in the inheritance hierarchy
  • Before superclass converted into subclasses (downward transition), should be checked using instanceof
  • Type conversion is not a good practice, polymorphic objects downcast is to subclass-specific method, in which case you should check the superclass design is reasonable, perhaps to redesign the superclass and add the appropriate method is correct practice. Minimize the use of casts and instanceof

 

Guess you like

Origin www.cnblogs.com/betterluo/p/10959239.html