2019.09.27 essays (method call)

  1. Compiler View object type declarations and method names.

       假设调用的是x.f(param),x为C类隐式参数对象。编译器会一一列举C类中所有名为f的方法和其超类中访问属性为public且名为f的方法。(超类的私有方法不可访问)
public void raiseSalary(double byPercent) {    
    double raise = salary * byPercent/100;    
    salary += raise;
}

Wherein this.salary implicit parameter, i.e. class objects in front of the method name appears. byPercent for the display parameters. Obviously, explicit parameters is evident in the column method, a hidden parameter does not appear in the process.

  1. The compiler will look at the type of parameters supplied when calling the method.

If there is argument type matching process in a method called f all types of parameters provided by the calling method, on the choice of this method, this process is called overload resolution . If the call xf ( "Hello"), the compiler will pick f (String).

  1. Static binding : If the method is private, static methods, final method or constructor. The compiler will know exactly should call that method, we will call this way is called static binding.

    Dynamic binding : the actual method call depends on the type of the implicit parameter, and the realization that method call at runtime, we call this is called dynamic binding.

  2. When the program is run, and dynamic binding method is called virtual machine must call a method most suitable for the actual type of the object referenced by that class of x, assuming x is the actual type D, which is a subclass of Cr, if the Class D defines a method f (String), call it directly, otherwise it will look for f (String) in the superclass Father in class D, and so on. I.e. search from subclass to superclass.

Guess you like

Origin www.cnblogs.com/njuptzheng/p/11600950.html