Help to understand the issue with protected method(帮助理解受保护方法的问题)
问题描述
我正在阅读 Sybex Complete Java 2 Certification Study Guide 2005 年 4 月 (ISBN0782144195).本书适用于希望通过java认证的java开发者.
I'm reading Sybex Complete Java 2 Certification Study Guide April 2005 (ISBN0782144195). This book is for java developers who wants to pass java certification.
在关于访问修饰符(以及其他修饰符)的一章之后,我发现了以下问题(#17):
After a chapter about access modifiers (along with other modifiers) I found the following question (#17):
判断对错:如果类 Y 扩展X班,两个班在不同的包,X 类有一个称为 abby() 的受保护方法,然后Y 的任何实例都可以调用 abby()Y 的任何其他实例的方法.
True or false: If class Y extends class X, the two classes are in different packages, and class X has a protected method called abby(), then any instance of Y may call the abby() method of any other instance of Y.
这个问题让我有点困惑.
This question confused me a little.
据我所知,您可以在同一类(或子类)的任何变量上调用受保护的方法.您不能在层次结构中高于您的变量上调用它(例如您实现的接口).
As far as I know you can call protected method on any variable of the same class (or subclasses). You cannot call it on variables, that higher in the hierarchy than you (e.g. interfaces that you implement).
例如,你不能仅仅因为你继承它而克隆任何对象.
For example, you cannot clone any object just because you inherit it.
但是这些问题没有提到变量类型,只提到了实例类型.
But the questions says nothing about variable type, only about instance type.
我有些疑惑,回答是".
I was confused a little and answered "true".
书中的答案是
错误.从不同包中的超类继承受保护方法的对象可以在其自身上调用该方法,但不能在同一类的其他实例上调用.
False. An object that inherits a protected method from a superclass in a different package may call that method on itself but not on other instances of the same class.
这里没有关于变量类型,只有实例类型.
There is nothing here about variable type, only about instance type.
这很奇怪,我不明白.
谁能解释这里发生了什么?
Can anybody explain what is going on here?
推荐答案
判断对错:如果类 Y 扩展类 X,这两个类在不同的包中,并且类 X 有一个名为 abby() 的受保护方法,那么 Y 的任何实例都可以调用任何其他实例的 abby() 方法Y.
True or false: If class Y extends class X, the two classes are in different packages, and class X has a protected method called abby(), then any instance of Y may call the abby() method of any other instance of Y.
错误.从不同包中的超类继承受保护方法的对象可以在其自身上调用该方法,但不能在同一类的其他实例上调用该方法".
"False. An object that inherits a protected method from a superclass in a different package may call that method on itself but not on other instances of the same class".
让我们把它写下来,就像 BalusC 做了,并向 Y 添加了一个方法,该方法调用 Y 的任何其他实例的 abby():
Let's write that down, as BalusC did, and add to Y a method which calls the abby() of any other instance of Y:
package one;
public class X {
protected void abby() {
}
}
package other;
import one.X;
public class Y extends X {
public void callAbbyOf(Y anyOther) {
anyOther.abby();
}
}
Y 可以调用它所引用的任何 Y 实例的 abby() 方法.所以书中的答案是明显错误的. Java 没有特定于实例的作用域(与具有实例私有作用域的 Scala 不同).
It is possible for Y to call the abby() method of any instance of Y to which it has a reference. So the answer in the book is blatantly wrong. Java does not have instance-specific scopes (unlike for example Scala which has an instance-private scope).
如果我们试图变得仁慈,也许这个问题的意思是说任何 Y的其他实例".它可以访问 恰好在内存中的任何 Y 实例的方法 - 这是不可能的,因为 Java 没有直接的内存访问.但在那种情况下,这个问题的措辞是如此糟糕,以至于你甚至可以回答:错误.您不能在不同 JVM 上的实例、已被垃圾回收的实例或一年前死亡的 JVM 上的实例等上调用方法."
If we try to be merciful, maybe the question meant by saying "any other instance of Y" that can it access the method of any instance of Y which happens to be in memory - which is not possible, because Java does not have direct memory access. But in that case the question is so badly worded, that you could even answer: "False. You can not call methods on instances which are on a different JVM, or instances which have been garbage collected, or instances on a JVM which died one year ago etc."
这篇关于帮助理解受保护方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:帮助理解受保护方法的问题
基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01