Java中哪些方法是动态绑定的?
问题说的是,Java 中哪些方法是动态绑定的?
来自 C++,如果我没记错的话,大多数方法都是静态绑定的,但有一些例外。
What the question says, which methods are dynamically bound in Java?
Coming from C++, if I am not mistaken, most methods are statically bound with a few exceptions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
理论上,所有方法都是动态绑定的,除了
实际上,在运行时,JVM 可能会选择 JIT 编译一些要静态解析的方法调用,例如,如果没有加载包含以下内容的类:一个压倒一切的方法。
In theory, all methods are dynamically bound, with the exception of
In practice, at runtime the JVM may choose to JIT-compile some method calls to be statically resolved, for instance if there are no loaded classes containing an overriding method.
实例方法调用在运行时解析,静态方法调用在编译时解析。
Instance method calls are resolved at runtime, static method calls are resolved at compile time.
一般来说,你可以这样想:
在编译时,编译器检查静态绑定。
在运行时检查动态类型。
例如:
*请注意,在最后一种类型转换中,编译器仅检查继承的可能性,而不检查是否存在 B 对象。
例如:
将编译但抛出运行时异常。
In general you can think of it like thie:
At compile time the compiler checks the static binding.
At runtime the dinamic type is checked.
for Example:
*notice that in the last case of casting, the compiler checks only the possibility of inheritance and not that there will be a B object.
for example:
will compile but throw a runntime exception.