- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我知道 child 不能降低非静态方法的可见性,我理解为什么会这样。
不过我读过“静态方法可以通过其重新声明来隐藏”。然而,我不明白如何在 Java 中实现这一点。
这真的可能吗?如果是,怎么做到的(代码示例),为什么引入(这似乎与不降低界面可见性的原则相矛盾)?
最佳答案
简短的回答是:不,这是不可能的。你混淆了一些术语。 隐藏与可访问性无关(这是您真正要问的,而不是可见性,它与范围 和 阴影 并在 Chapter 6 of the Java Language Specification (JLS) 中讨论)。
现在是更长的答案。术语覆盖 适用于实例方法,而术语隐藏 适用于类 (static
) 方法。来自Java Tutorial topic Overriding and Hiding Methods :
The distinction between hiding a static method and overriding an instance method has important implications:
- The version of the overridden instance method that gets invoked is the one in the subclass.
- The version of the hidden static method that gets invoked depends on whether it is invoked from the superclass or the subclass.
这里的一些其他答案提供了关于方法隐藏的不正确示例,所以让我们回到 JLS,这次是 §8.4.8 :
Methods are overridden or hidden on a signature-by-signature basis.
也就是说,要覆盖或隐藏父类中的方法,子类必须定义具有相同签名的方法——基本上,相同数量和类型的参数(尽管泛型和类型删除使规则比这复杂一点)。还有关于返回类型和 throws
子句的规则,但这些似乎与这个问题无关。
请注意,您可以在子类中定义一个方法,其名称 与父类(或已实现的接口(interface))中的方法相同,但参数的数量或类型不同。在那种情况下,您重载方法名称,既不覆盖也不隐藏任何东西;子类方法是一种新方法,几乎独立于继承的方法。 (当编译器必须将方法与方法调用相匹配时会发生交互,仅此而已。)
现在回答您的问题:术语可访问性 和隐藏(以及可见性)在Java 中是独立的概念。正如您所说,有一个“原则”,即子类根本无法减少继承方法的可访问性。无论您是覆盖实例方法还是隐藏类方法,这都适用。来自 the JLS §8.4.8.3 :
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:
If the overridden or hidden method is
public
, then the overriding or hiding method must bepublic
; otherwise, a compile-time error occurs.If the overridden or hidden method is
protected
, then the overriding or hiding method must beprotected
orpublic
; 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
方法可以隐藏与改变方法的可访问性无关。
关于java - 降低静态方法的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963828/
我在堆栈上创建了这段代码: function increase_brightness(hex, percent){ var r = parseInt(hex.substr(1, 2), 16)
为什么我能够LOWER COALESCE 中的每个项目,但无法LOWER整个COALESCE,否则我会遇到语法错误?例如: SELECT COALESCE(LOWER(google_provider_
我在谷歌上搜索到的所有内容都表明,以下任何一项都会将 double 舍入到小数点后两位。 double roundToFourDecimals(double d) { DecimalForma
我正在开发一个 flexdashboard/storyboard,我想在其中降低每个帧的高度。那可能吗? 示例代码: --- title: "Flex" output: flexdashboard
我在 WPF 中有一个图像控件。我需要减小图像尺寸控件的宽度和高度。但是当我这样做时,图像看起来不太好。数据丢失更多。 所以我想降低图像分辨率而不是仅仅改变图像控件的宽度和高度。 任何人都可以帮助我如
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题?通过 editing this post 添加详细信息并澄清问题. 1年前关闭。 Improve this
我正在扩展 Fluent NHibernate,以便更好地与 F# 一起使用(即引用支持),并希望获得一些关于降低 API 流畅性的反馈。 F# 要求使用返回值,除非它们是单位类型。所以这最终以“|>
我们有一个 BizTalk 2010 接收位置,它将获取一个 70MB 的文件,然后使用入站映射(在接收位置)和出站映射(在发送端口)生成一个 1GB 文件。 执行上述过程时,SQL Server 会
我的代码分析插件提示包含以下代码的方法中的代码复杂性。我注意到以下代码看起来可以组合,但我不知道如何做到这一点: for(Command command : commands) { if (c
我正在寻找一种方法来始终忽略 R 中 float 之间的微小差异(根据 IEC 60559,这些是 double 浮点),通过使用基本 R 工具而不诉诸 C 或 C++。换句话说,我想“四舍五入” d
在 Blazor 中使用 ChartJs.Blazor 的 BarChart 组件时是否可以降低甚至关闭动画速度?我发现这个 NuGet 包非常有用,但我不知道如何在更新条形图时关闭动画。为了更容易忽
所以我为一个游戏编写了这段代码,现在该游戏的速度非常快。我想降低 FPS,让游戏慢一点。 我认为我唯一的出路就是制作一个计时器。但我发现很难找到放置计时器的位置?谁能帮我解决这个问题吗? 所以我为一个
我正在编写一个程序,我担心它运行所需的时间和所占用的空间。 在程序中我使用了一个变量来存储数组的长度: int len=newarray3.length; 现在,我想知道是否能够通过不使用 len 变
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 2 年前。 Improve th
我用Java编写了一个程序,但它的计算时间很长,我不知道为什么。有人可以指点一下以降低复杂性吗?此外,在计算一些值(例如 3,100 之后)后,它会给出空指针异常。代码: public class F
我有下图,由 1 行 2 列的网格组成。我愿意 降低右侧子图的高度(3D PREDICTION),使棋盘平面看起来有点挤压并显示更好的视角。 在左侧子图的顶部添加一些边距(2D PREDICTION)
是否有一种简单的方法可以更改以 RGB 字符串形式给出的颜色的亮度? 例如 in_RGB = '#FF0000' --> out_RGB = '#CC0000' 最佳答案 将十六进制字符串转换为 R
我已经编写了代码来更改对象(不是进程)(在本例中是文件)的完整性级别。据我们所知,我们从中等完整性级别开始,但我想将其降低到“低”。我想运行完整性较低的 .txt 文件而不是默认介质。 我使用 WIN
是否可以在保持原始宽高不变的情况下降低图像分辨率? 我已经使用 BitmapFactoryOptions 尝试了几个选项: 在样本大小 inDensity、inScaled、inTargetDensi
是否有高级(Java)或低级方式(使用 native 代码)将 Android 设备上的蓝牙信号强度更改为最低? 目标是使设备在 20 厘米范围内可被发现?在 Internet 上根本找不到与此相关的
我是一名优秀的程序员,十分优秀!