如果这个 resolution is private, static, final or constructor,那就开 call。这个过程叫 static binding解读一下:能用 private 方法的能有谁?好像只有自己这个类吧,经测试,就是使用的当前方法所在类的对应私有方法。而 static 搞不清楚,确实是就算多态的情况下,也是按照声明的类型中的静态方法调用 但要是是别的情况,那就要按照 actual type 来调用函数了 (找不到就往父层找) 找了就开 call,这个过程叫 dynamic binding
It would be time-consuming to carry out this search every time a method is called. Instead, the virtual machine precomputes for each class a method table that lists all method signatures and the actual methods to be called.
If a method is not overridden, and it is short, then a compiler can optimize the method call away—a process called inlining.
Dynamic Binding
而这种能够正确的选出对应的方法的情况就叫 dynamic binding
知道为什么这么个东西还要命个名给它一个好像很牛逼的动态绑定了,因为这好像确实是个挺牛逼的东西。根据意思,这个多态说到底始终还是个父类,能调用的也只有父类有的方法,虽然 refer to 一个子类,但是子类的东西它是一个也不能调用,那按照这个道理,当调用被 override 的方法时,应该是父类的呀,结果它又能动态绑定子类了😅
In C++, you need to declare a member function as virtual if you want dynamic binding. In Java, dynamic binding is the default behavior; if you do not want a method to be virtual, you tag it as final. (We discuss the final keyword later in this chapter.)
A Very Important Property
It makes programs extensible without the need for modifying existing code.