可以在子类中声明更私有(private) ... 最佳答案 Methods d-6ren">
gpt4 book ai didi

java - Java中 "methods without access control can be declared more private in subclasses"是什么意思?

转载 作者:行者123 更新时间:2023-12-01 11:31:35 24 4
gpt4 key购买 nike

在java访问修饰符和继承主题中说了以下4点。第三点中的“更私密”是什么意思?

  1. ...
  2. ...
  3. 声明的方法没有访问控制=>可以在子类中声明更私有(private)
  4. ...

最佳答案

Methods declared without access control => can be declared more private in subclasses

这是错误的。情况恰恰相反。使用任何级别的访问控制声明的方法都必须使用该级别或更公共(public)的级别进行覆盖:在这种情况下,访问级别基类是“默认”,因此您可以使用 public 进行覆盖> protected 。

与 Google 文档所述相反,您不能使用 private 覆盖。 请参阅 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 be public; otherwise, a compile-time error occurs.
  • 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 package access, then the overriding or hiding method must not be private; otherwise, a compile-time error occurs.

关于java - Java中 "methods without access control can be declared more private in subclasses"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30344548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com