JAVA中虚方法的调用(Virtual Method Invocation)
2014-04-02 22:30
429 查看
在多态的情况下,声明为父类类型的引用变量只能调用父类中的方法,但如果此变量实际引用的是子类对象,而子类对象中覆盖了父类的方法,这时父类对象调用的是子类中的方法,这种机制就成为虚方法调用。
所以,同样的两个引用变量调用相同的方法结果可能不同。
In Java, all non-static methods are by default "virtual functions." Only methods marked with the keyword final, which cannot be overridden, along with private methods, which are
not inherited, are non-virtual.
Yes, virtual methods are treated differently by the compiler and the runtime. The JVM specifically utilizes a virtual
method table for virtual method dispatch:
An object's dispatch table will contain the addresses of the object's dynamically bound methods. Method calls are performed by fetching the method's address from the object's dispatch table. The dispatch table is the same for all objects belonging to the same
class, and is therefore typically shared between them. Objects belonging to type-compatible classes (for example siblings in an inheritance hierarchy) will have dispatch tables with the same layout: the address of a given method will appear at the same offset
for all type-compatible classes. Thus, fetching the method's address from a given dispatch table offset will get the method corresponding to the object's actual class.
所以,同样的两个引用变量调用相同的方法结果可能不同。
In Java, all non-static methods are by default "virtual functions." Only methods marked with the keyword final, which cannot be overridden, along with private methods, which are
not inherited, are non-virtual.
Yes, virtual methods are treated differently by the compiler and the runtime. The JVM specifically utilizes a virtual
method table for virtual method dispatch:
An object's dispatch table will contain the addresses of the object's dynamically bound methods. Method calls are performed by fetching the method's address from the object's dispatch table. The dispatch table is the same for all objects belonging to the same
class, and is therefore typically shared between them. Objects belonging to type-compatible classes (for example siblings in an inheritance hierarchy) will have dispatch tables with the same layout: the address of a given method will appear at the same offset
for all type-compatible classes. Thus, fetching the method's address from a given dispatch table offset will get the method corresponding to the object's actual class.
相关文章推荐
- Java Remote Method Invocation(Java远程方法调用)
- Java Remote Method Invocation(Java远程方法调用)-- Java分布式计算白皮书
- Java RMI 远程方法调用 (Remote Method Invocation)
- Spring之RMI 远程方法调用 (Remote Method Invocation)
- 远程方法调用(Remote Method Invocation,RMI)
- RMI(Remote Method Invocation,远程方法调用)
- 远程方法调用(Remote Method Invocation,RMI)
- Java中通过method对象来调用方法
- struts.enable.DynamicMethodInvocation = true 动态方法调用(转)
- RMI(Romte Method Invocation):远程方法调用
- Delphi调用Java的WebService报错can not find dispatch method for{null}terminalNo using "Payload QName-based Dispatcher"的解决方法
- java中通过反射获取方法并且调用(getMethod和invoke深入)实践
- JNI调用native方法出现 java.lang.UnsatisfiedLinkError: XXXclass.XXXmethod()异常的解决办法
- struts.enable.DynamicMethodInvocation = true 动态方法调用(转)
- struts.enable.DynamicMethodInvocation = true 动态方法调用
- struts.enable.DynamicMethodInvocation = true 动态方法调用
- Java远程调用RMI(Remote Method Invocation)
- Java中反射调用私有方法出现NoSuchMethodException
- struts.enable.DynamicMethodInvocation = true 动态方法调用
- Java调用重载方法(invokevirtual)和接口方法(invokeinterface)的解析