java java中的所有方法都是隐式虚拟的吗
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12752343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Are all method in java implictly virtual
提问by Arun
If there are no compile time binding in java, do this mean all binding are done at runtime?
如果java中没有编译时绑定,这是否意味着所有绑定都是在运行时完成的?
But as per OOPs concept for runtime binding, functions must have virtual keyword..ARE all methods implicitly virtual in java or is there any compile time binding exist in java
但是根据运行时绑定的 OOPs 概念,函数必须有 virtual 关键字..Java 中的所有方法都是隐式虚拟的,或者 Java 中是否存在任何编译时绑定
If there is any compile time binding, can you give me some specific situation, or links to further information
如果有任何编译时绑定,你能给我一些具体的情况,或者更多信息的链接
- Static (There is no meaning of binding here as static does not belongs to object)
- final (this is not a valid point as it can be achived in another way)
- 静态(这里没有绑定的意思,因为静态不属于对象)
- 最终(这不是一个有效的点,因为它可以通过另一种方式实现)
回答by Tomasz Nurkiewicz
All non-static
, non-final
and non-private
methods are virtual by default in Java. However JVM is clever enough to find classes having only one implementation of given method and turn it into static binding.
所有非static
、非final
和非private
方法在 Java 中默认都是虚拟的。然而,JVM 足够聪明,可以找到只有一种给定方法实现的类,并将其转换为静态绑定。
This way you don't have to remember about virtual
keyword (ever experienced memory leak due to missing virtual
on destructor in C++?) while the performance is not impacted that much.
这样您就不必记住virtual
关键字(是否曾因virtual
C++ 中的析构函数丢失而导致内存泄漏?),而性能不会受到太大影响。
回答by emesx
Non-static method invocation is the main (only) dynamic aspect of Java. All methods are virtual in Java. This does not apply to static methods, which are bound at compile time, based on the static type of object.
非静态方法调用是 Java 的主要(唯一)动态方面。Java 中的所有方法都是虚拟的。这不适用于基于对象的静态类型在编译时绑定的静态方法。
回答by IamVickyAV
Methods which we can't override in sub class are generally called non virtual methods.
我们不能在子类中覆盖的方法通常称为非虚拟方法。
In Java static, private & final methods are non virtual by default. Other methods are virtual by default.
在 Java 静态中,私有和最终方法默认是非虚拟的。其他方法默认是虚拟的。