降低静态方法的可见性 [英] Reducing the visibility of a static method

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

问题描述

我知道孩子不能降低非静态方法的可见性,我理解为什么会这样。

I know that a child cannot reduce the visibility of a non-static method and I understand why it is so.

我读过静态方法可以通过重新声明来隐藏。但是我不明白如何用Java实现这一目标。

I've read however that "static method can be hidden through its redeclaration". I however do not understand how this could be achieved in Java.

这真的有可能吗?如果是,那该怎么做(代码示例)以及它为什么被引入(它似乎与非减少接口可见性的原则相矛盾)?

Is this really possible? If yes, how to do that (code example) and why was it introduced (it seems to contradict the principle of non-reducing the visibility of the interface)?

推荐答案

简短的回答是:不,这是不可能的。你混淆了一些术语。 隐藏辅助功能无关(这是您真正要求的,而不是可见性,与范围<相关 shadowing 并在 Java语言规范(JLS)的第6章

The short answer is: no, it is not possible. You have mixed up some terminology. Hiding has nothing to do with accessibility (which is what you are really asking about, not visibility, which is related to scope and shadowing and is discussed in Chapter 6 of the Java Language Specification (JLS)).

现在获得更长的答案。术语覆盖适用于实例方法,而术语隐藏适用于类(静态)方法。来自 Java教程主题覆盖和隐藏方法

Now for the longer answer. The term overriding applies to instance methods, while the term hiding applies to class (static) methods. From the Java Tutorial topic Overriding and Hiding Methods:


隐藏静态方法和覆盖实例方法之间的区别具有重要意义:

The distinction between hiding a static method and overriding an instance method has important implications:


  • 被调用的被重写实例方法的版本是子类中的一个。

  • 被调用的隐藏静态方法的版本取决于它是否是从超类或子类调用。

这里的一些其他答案提供了有关方法的错误示例隐藏,所以让我们回到JLS,这次是§8.4.8

Some of the other answers here provide incorrect examples about method hiding, so let's go back to the JLS, this time to §8.4.8:


按签名方式覆盖或隐藏方法。

Methods are overridden or hidden on a signature-by-signature basis.

Th在is,要覆盖或隐藏父类中的方法,子类必须定义一个具有相同签名的方法—基本上,相同数量和类型的参数(尽管泛型和类型擦除使得规则比那更复杂)。还有关于返回类型和 throws 子句的规则,但这些似乎与此问题无关。

That is, to override or hide a method in the parent class, the subclass must define a method with the same signature—basically, the same number and type of arguments (although generics and type erasure makes the rules a little more complicated than that). There are also rules about return types and throws clauses, but those seem irrelevant to this question.

请注意可以在子类中定义一个方法,该方法具有与父类(或实现的接口)中的方法相同的 name ,但具有不同数量或类型的参数。在这种情况下,你重载方法名称,既不覆盖也不隐藏任何东西;子类方法是一种新方法,几乎​​与继承的方法无关。 (当编译器必须将方法与方法调用匹配时,存在交互,但这就是它。)

Note that you can define a method in a subclass with the same name as a method in the parent class (or in an implemented interface) but with different number or type of arguments. In that case, you are overloading the method name and neither overriding nor hiding anything; the subclass method is a new method, pretty much independent of the inherited method(s). (There is an interaction when the compiler has to match methods to method calls, but that's about it.)

现在问题:术语可访问性隐藏(以及 visibility )是Java中的独立概念。正如你所说,有一个原则,即子类根本无法减少继承方法的可访问性。无论您是重写实例方法还是隐藏类方法,这都适用。来自JLS§8.4.8.3

Now to your question: the terms accessibility and hiding (as well as visibility) are independent concepts in Java. There is, as you put it, a "principle" that there is simply no way for a subclass to reduce the accessibility of an inherited method. This applies regardless of whether you are overriding an instance method or hiding a class method. From the JLS §8.4.8.3:


访问修饰符(§6.6必须至少提供与重写或隐藏方法一样多的访问权限,如如下:

The access modifier (§6.6) of an overriding or hiding method must provide at least as much access as the overridden or hidden method, as follows:


  • 如果被覆盖或隐藏的方法是 public ,那么覆盖或隐藏方法必须 public ;否则,发生编译时错误。

  • If the overridden or hidden method is public, then the overriding or hiding method must be public; otherwise, a compile-time error occurs.

如果被覆盖或隐藏的方法是 protected ,那么覆盖或隐藏方法必须 protected public ;否则,发生编译时错误。

If the overridden or hidden method is protected, then the overriding or hiding method must be protected or public; otherwise, a compile-time error occurs.

如果重写或隐藏方法具有默认(包)访问权限,则覆盖或隐藏方法必须私人;否则,发生编译时错误。

If the overridden or hidden method has default (package) access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.

总之,事实是可以隐藏 static 方法与更改方法的可访问性无关。

In summary, the fact that a static method can be hidden has nothing to do with changing the accessibility of the method.

这篇关于降低静态方法的可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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