帮助理解与保护方法的问题 [英] Help to understand the issue with protected method

查看:145
本文介绍了帮助理解与保护方法的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读 Sybex Complete Java 2认证考试指南2005年4月(ISBN0782144195)。



在有关访问修饰符(以及其他修饰符)的章节后,我发现了以下问题(#17):


True或false:如果Y类扩展
X类,这两个类在
不同的包中,类X有一个
受保护的方法称为abby(),然后
任何Y的实例可以调用任何其他Y实例的abby()
方法。


这个问题让我很困惑。



据我所知,你可以在任何变量上调用protected方法的同一类(或子类)。你不能在变量上调用它,这个变量在层次结构中比你更高(例如你实现的接口)。



例如,你不能克隆任何对象, 。



但问题什么也不说变量类型,只是关于实例类型。



我很困惑,回答真。



书中的答案是


False。从不同包中的超类继承受保护方法的对象可以在自身上调用该方法,但不能在同一类的其他实例上调用。






这是很奇怪的,我不明白。



任何人都可以解释这里发生了什么?

解决方案

false:如果类Y扩展类X,这两个类在不同的包中,类X有一个被称为abby()的受保护的方法,则任何Y的实例可以调用Y的任何其他实例的abby()方法。 p>

False。从不同包中的超类继承受保护方法的对象可以在自身上调用该方法,但不能在同一类的其他实例上调用。


让我们写下来,如 BalusC ,并添加到Y调用任何其他Y实例的abby()的方法:

 包一; 
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有一个实例 - 私有范围)。



如果我们试图宽恕,也许这个问题的意思是说任何 Y的其他实例,它可以访问任何Y的实例的方法内存 - 这是不可能的,因为Java没有直接内存访问。但在这种情况下,问题是如此严重的措辞,你甚至可以回答:False。你不能调用在不同的JVM上的实例,或者已经被垃圾收集的实例,或在JVM上的实例死了一个一年前等。


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.

After a chapter about access modifiers (along with other modifiers) I found the following question (#17):

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".

The answer in the book is

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.

This is very strange, I do not understand it.

Can anybody explain what is going on here?

解决方案

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".

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();
    }
}

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).

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."

这篇关于帮助理解与保护方法的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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