覆盖静态方法和最终方法 [英] override of static method and final method

查看:114
本文介绍了覆盖静态方法和最终方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道 Java static 方法不能被子类覆盖。

I know in Java, static method can not be overriden by the subclass.

两个问题:

1。为什么?有人能解释一下里面的原因吗?

1. Why is that? Could anyone explain me the reason inside it?

2. 子类覆盖 final 超类中的方法呢?

2. Can subclass override final method in super class then?

推荐答案

在特定实例上不调用静态方法 - 所以它们不能被称为多态。它们在类型本身上被调用 - 绑定的任何内容都不依赖于任何仅在执行时可用的信息。关于多态调用的观点是最终执行的方法实现取决于调用目标的执行时类型;静态方法调用没有目标。

Static methods aren't called on a particular instance - so they can't be called polymorphically. They are called on the type itself - nothing about the binding relies on any information which is only available at execution time. The point about polymorphic calls is that the method implementation which ends up being executed depends on the execution-time type of the target of the call; there's no target for static method calls, as such.

不,子类不能覆盖最终方法 - 使方法最终的最重要的一点是防止它被被覆盖。

No, subclasses can't override final methods - the whole point of making a method final is to prevent it from being overridden.

这篇关于覆盖静态方法和最终方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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