java - 我可以在 Java 中覆盖和重载静态方法吗?

标签 java static overloading overriding

我想知道:

  1. 为什么不能在 Java 中覆盖静态方法?
  2. Java 中可以重载静态方法吗?

最佳答案

静态方法不能按字面意思被覆盖,但它们可以隐藏父静态方法

在实践中,这意味着编译器将在编译时决定执行哪个方法,而不是在运行时决定执行,因为它与重写的实例方法一样。

查看一个简洁的示例 here .

还有 this是解释 覆盖 实例方法和 隐藏 类(静态)方法之间区别的 java 文档。

Overriding: Overriding in Java simply means that the particular method would be called based on the run time type of the object and not on the compile time type of it (which is the case with overriden static methods)

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 - 我可以在 Java 中覆盖和重载静态方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2475259/

相关文章:

java - 删除旧的 .lck 文件

c++ - 静态变量的地址值可以与堆分配地址匹配吗?

c++ - 在 main(...) 而不是全局中初始化类的静态常量数组成员?

java - 转换参数如何影响 java 中的覆盖和重载方法?

java - 正则表达式来验证字符串

java - 使用运算符 "length"无法在类 "java.lang.String"的对象中找到 "."的值

java - 通过 ajax 调用调用操作方法时出现意外行为

php - 缓存/离线网站脚本

scala - 重载函数导致 "missing parameter type for expanded function"错误

c++ - 如何在 C++ 中创建一个 "default"流插入运算符?