问题描述
我想知道:
- 为什么不能在Java中覆盖静态方法?
- 静态方法可以在Java中重载吗?
推荐答案
静态方法不能在单词的确切意义上重写,但它们可以隐藏父静态方法
Static methods can not be overridden in the exact sense of the word, but they can hide parent static methods
实际上它意味着que编译器将决定在编译时执行哪个方法,而不是在运行时,就像重写实例方法一样。
In practice it means that que compiler will decide which method to execute at compile time, and not in runtime, as it does with overridden instance methods.
对于一个简洁的例子看看。
For a neat example have a look here.
和是java文档,解释了覆盖实例方法和隐藏之间的区别class(静态)方法。
And this is java documentation explaining the difference between overriding instance methods and hiding class (static) methods.
隐藏:静态的父类方法不属于子类(尽管它们是可访问的),因此不存在$ b $的问题b压倒它。即使在子类中添加另一个静态方法,
与其父类中的静态方法相同,此子类静态方法
也是唯一的,与其父类中的静态方法不同。
Hiding: Parent class methods that are static are not part of a child class (although they are accessible), so there is no question of overriding it. Even if you add another static method in a subclass, identical to the one in its parent class, this subclass static method is unique and distinct from the static method in its parent class.
这篇关于我可以覆盖和重载Java中的静态方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!