Static binding and dynamic binding

First, bind

  A method and class / object linked.

Second, the static binding

  If what method private, static, final method, the compiler can accurately guide should call, because subclasses can not override these methods, this method calls the static binding (static binding).

Third, dynamic binding

  Method call depends on the actual type of a hidden parameter, and dynamic run-time binding.

Fourth, the dynamic binding process

  Virtual machine first look at the class of the object in the method if there is, if there is a direct call, if not then looking in the superclass. But if the search should be carried out every call method, time overhead can be significant. Therefore, a virtual machine for each class previously created a method table, which lists the actual method signature and call all methods.
  Employee.java

 1 package test;
 2 import java.time.*;
 3 public class Employee {
 4     private String name;
 5     private double salary;
 6     private LocalDate hireDay;
 7 
 8     public Employee(String name, double salary, int year, int month, int day){
 9         this.name = name;
10         this.salary = salary;
11         this.hireDay = LocalDate.of(year,month,day);
12     }
13 
14     public Employee(){
15 
16     }
17     public String getName() {
18         return name;
19     }
20 
21     public double getSalary() {
22         return salary;
23     }
24 
25     public LocalDate getHireDay() {
26         return hireDay;
27     }
28 
29     public void raiseSalary(double byPercent) {
30         double raise = salary * byPercent / 100;
31         salary += raise;
32     }
33 
34 
35 
36 }

  Manager.java:

 1 package test;
 2 
 3 public class Manager extends Employee {
 4     private double bonus;
 5     public Manager(String name,double salary, int year, int month, int day){
 6         super(name,salary,year,month,day);
 7         bonus = 0;
 8     }
 9 
10     public Manager(){
11     }
12     
13     @Override
14     public double getSalary () {
 15          double baseSalary = super .getSalary ();
16          return baseSalary + bonus;
17      }
 18  
19      public  void setBonus ( double b) {
 20          bonus = b;
21      }
 22 }

  main.java

 1 package test;
 2 
 3 public class Main {
 4     public static void main(String[] args) {
 5         Manager boss = new Manager("Carl",8000,1987,12,15);
 6         boss.setBonus(5000);
 7         Employee[] staff = new Employee[3];
 8         
 9         staff[0] = boss;
10         staff[1] = new Employee("Harry", 5000, 1989, 10, 1);
11         staff[2] = new Employee("Harry", 4000, 1990, 3, 15);
12     
13         for(Employee e : staff)
14             System.out.println("name="+e.getName()+",salary="+e.getSalary());
15     }
16 
17 
18 }

  E.getSalary call on line 14 main.java () procedure:

  • Since getSalary not private, static methods, or final, so the dynamic binding.
  • Virtual Machine Manager in advance of any two class Employee table generation method.
    The Employee:
      getName () -> Employee.getName ()
      getSalary () -> Employee.getSalary ()
      getHireDay () -> Employee.getHireDay ()
      raiseSalary (Double) -> Employee.raiseSalary (Double)
    Manager:
      getName () - > Employee.getName () (Manager does not inherit from a parent class)
      getSalary () -> Manager.getSalary () (rewrite)
      getHireDay () -> Employee.getHireDay ()
      raiseSalary (Double) -> Employee.raiseSalary ( Double)
      setBonus (Double) -> Manager.setBonus (Double) (Manager method)

    Note: this method is omitted method table Object class, all classes inherited from class Object
  • First, the actual type of virtual machine extraction method table of e. The method may be either Employee table, the Manager, Employee table method may also be of other subclasses (in the present embodiment staff [0] is Manager, staff [1], staff [2] is Employee).
  • Next, search for the definition of virtual machine getSalary signatures. This method is called.

Guess you like

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