为什么我们不能覆盖静态和最终方法? [英] Why is it that we cannot override static and final methods?

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

问题描述

我试图理解为什么我们不能覆盖静态和最终方法。我没有得到它背后的目的。

I am trying to understand why we can't override static and final methods. I do not get the purpose behind it.

推荐答案

final 方法无法覆盖,因为那就是 final 旨在:它是一个标志,不要覆盖它。

final methods can't be overriden, because that's what final is designed to do: it's a sign saying "do not override this".

static 方法无法覆盖,因为它们永远不会以多态方式调用:当你调用 SomeClass.foo()时,它会总是 foo SomeClass 的方法,无论是否有另一个 ExtendedSomeClass 有一个更加常规的 foo 方法。

static methods can't be overriden, because they are never called in a polymorphic way: when you call SomeClass.foo() it will always be the foo method of SomeClass, no matter if there is another ExtendedSomeClass that has a much groovier foo method.

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

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