为什么Java不允许覆盖静态方法? [英] Why doesn't Java allow overriding of static methods?

查看:171
本文介绍了为什么Java不允许覆盖静态方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么无法覆盖静态方法?

Why is it not possible to override static methods?

如果可能,请使用示例。

If possible, please use an example.

推荐答案

重写取决于拥有一个类的实例。多态性的一点是,您可以对类进行子类化,并且实现这些子类的对象将对超类中定义的相同方法具有不同的行为(并在子类中重写)。静态方法不与类的任何实例相关联,因此该概念不适用。

Overriding depends on having an instance of a class. The point of polymorphism is that you can subclass a class and the objects implementing those subclasses will have different behaviors for the same methods defined in the superclass (and overridden in the subclasses). A static method is not associated with any instance of a class so the concept is not applicable.

推动Java设计的两个因素影响了这一点。一个是对性能的担忧:有很多人批评Smalltalk关于它太慢(垃圾收集和多态调用是其中的一部分)而Java的创建者决心避免这种情况。另一个决定是Java的目标受众是C ++开发人员。使静态方法的工作方式与C ++程序员熟悉,并且速度非常快,因为没有必要等到运行时才能找出要调用的方法。

There were two considerations driving Java's design that impacted this. One was a concern with performance: there had been a lot of criticism of Smalltalk about it being too slow (garbage collection and polymorphic calls being part of that) and Java's creators were determined to avoid that. Another was the decision that the target audience for Java was C++ developers. Making static methods work the way they do had the benefit of familiarity for C++ programmers and was also very fast, because there's no need to wait until runtime to figure out which method to call.

这篇关于为什么Java不允许覆盖静态方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆