简体   繁体   English

为什么java不允许为子类中的静态方法分配较弱的访问权限?

[英]Why doesn't java allow to assign weaker access privileges to static methods in a child class?

I understand why java doesn't allow to set weaker access privileges for overridden methods, but why is it the same for static methods? 我理解为什么java不允许为重写方法设置较弱的访问权限,但为什么静态方法是相同的呢? I mean, these methods only hide each others, right? 我的意思是,这些方法只能隐藏对方,对吗? So what's the problem from the encapsulation point of view? 那么从封装的角度来看问题是什么?

PS PS

I know that there are 5 rules for hiding a method 我知道隐藏方法有5条规则

  1. The method in the child class must have the same signature as the method in the parent class. 子类中的方法必须与父类中的方法具有相同的签名。
  2. The method in the child class must be at least as accessible or more accessible than the method in the parent class. 子类中的方法必须至少与父类中的方法一样可访问或更易于访问。
  3. The method in the child class may not throw a checked exception that is new or broader than the class of any exception thrown in the parent class method. 子类中的方法可能不会抛出一个新的或更广泛的检查异常,而不是父类方法中抛出的任何异常的类。
  4. If the method returns a value, it must be the same or a subclass of the method in the parent class, known as co-variant return types. 如果方法返回一个值,则它必须是父类中方法的相同或子类,称为共变量返回类型。
  5. The method defined in the child class must be marked as static if it is marked as static in the parent class (method hiding). 如果子类中定义的方法在父类中标记为static(方法隐藏),则必须将其标记为static。 Likewise, the method must not be marked as static in the child class if it is not marked as static in the parent class (method overriding). 同样,如果子类中的方法未在父类中标记为静态(方法重写),则不能在子类中将该方法标记为静态。

But after all, I don't get the idea from the encapsulation prospective 但毕竟,我从封装前景中得不到这个想法

相同的规则是有效的,并且隐藏方法https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.4.8.3

Static methods have special access permissions that allow friend access to members within a instance. 静态方法具有特殊的访问权限,允许朋友访问实例中的成员。 For example, you could have a static method called create that creates a default instance of the class and directly modifies data within the instance without using instance properties or instance methods (just for a usage example, not necessarily a usage recommendation). 例如,您可以使用名为create的静态方法create类的默认实例,并直接修改实例中的数据,而无需使用实例属性或实例方法(仅用于用法示例,不一定是用法建议)。

Because of this special access, lower access permissions to the static methods could allow you to create an object in a state that you could not otherwise use, or modify an object in an unpredictable and non-encapsulated way. 由于这种特殊访问,对静态方法的较低访问权限可以允许您创建一个您无法使用的状态的对象,或以不可预测和非封装的方式修改对象。

This is only a minor annoyance for most user-defined classes, but allowing this when sub-classing eg containers could expose serious undefined behavior problems. 对于大多数用户定义的类来说,这只是一个小麻烦,但是在对容器进行子类化时允许这样做可能会暴露严重的未定义行为问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Java不允许通过实例方法隐藏静态方法? - Why doesn't Java allow hiding static methods by instance methods? "为什么Java不允许覆盖静态方法?" - Why doesn't Java allow overriding of static methods? 泛型问题:clone()尝试分配较弱的访问权限 - Generics issue: clone() attempting to assign weaker access privileges Android AsyncTask错误,尝试分配较弱的访问权限。 - Android AsyncTask error, attempting to assign weaker access privileges. 无法覆盖 Fragment 中的 onResume() 尝试分配较弱的访问权限; 是公开的 - Cannot override onResume() in Fragment attempting to assign weaker access privileges; was public 试图分配较弱的访问权限; 是isCancelled的公开错误 - attempting to assign weaker access privileges; was public error for isCancelled java试图分配较弱的访问权限错误 - java attempting to assign weaker access privilege error 为什么java不允许在父类中使父类的实例方法更具限制性 - Why java doesn't allow to make an instance method of parent class as more restrictive in child class Java帮助:为什么我不能访问当前主方法文件之外的类中声明的静态方法 - Java Help: why can't I access the static methods declared in a class outside the current main method file 为什么我们不能在子类中分配较弱的权限 - why can't we assign weaker privilege in subclass
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM