java-based: dynamic binding process polymorphism

Heavy brush of chapter05 java-core, P158

Rereading polymorphism, I do not feel the same.

Record it during the execution of an object method:

1. compiler view object type declarations and method names, such as class.fuction (param), class A declared object lists function (param) A and all the parent class's public function (param).

Employee {
...
getSalary(){} } Manager extends Employee{ ...
getSalary(){} }
Manager managerA
Employee e =managerA
e.getSalary(){}

  This lists of e getSalary, public e parent class of getSalary, getSalary all subclasses of e.

 

2. overload resolution overloading resolution: supplied argument type when viewing the calling method. See param int or as a String, by allowing conversion type, such as int converted into double, son, etc. can be converted into a father, or a plurality of parameters found no matching error.

3. If when private / static / final method, or constructor, then the compiler know exactly which method to call, call this way become static binding static binding. This corresponds to process call depends on the actual type of a hidden parameter, and dynamic run-time binding, generates a call fuction (param) instruction.

4. When the program is run, and dynamic binding method is called virtual machine must call the object's class referenced with the actual type of the most suitable method of that class. Assuming the actual type of class son, which is a subclass of the father. If the son is defined fuction (param), direct call; otherwise, call the father of the public function (param), and so on.

   Examples 1, e is the actual type of the subclass object Employee Manager, the Manager getSalary so call ().

 

As the search method call, it takes time. The opportunity to create a virtual method table for each class, to find time directly from the table inside.

 

Guess you like

Origin www.cnblogs.com/zhizhiyin/p/11095703.html