为什么我不能调用从继承类另一个包在Java中一个受保护的方法是什么? [英] Why can't I call a protected method from an inheriting class in another package in Java?

查看:753
本文介绍了为什么我不能调用从继承类另一个包在Java中一个受保护的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说有下面的基类:

 包bg.svetlin.ui.controls;公共抽象类控制{
    保护INT的getHeight(){
        // ..
    }
    // ...
}

此外,在同一个包,有继承的类:

 包bg.svetlin.ui.controls;公共抽象类LayoutControl扩展控制{
    公共抽象无效的AddControl(控制控制);
    // ...
}

然后,是另一个包三等:

 包bg.svetlin.ui.controls.screen;公共抽象类屏幕扩展LayoutControl {
    // ...
}

最后,还有在不同的包的实现类,又说:

 包bg.svetlin.ui.controls.screen.list;公共类列表扩展屏幕{    私人最终载体对照=新的向量();    公共无效的AddControl(控制控制){
        身高+ = control.getHeight();
        controls.addElement(对照组);
    }
}

虽然列表继承控制的getHeight()为保护 ,有以下错误:


  

的getHeight()已在受保护访问bg.svetlin.ui.controls.Control


我检查了我的进口是正确的。我使用NetBeans。

任何想法有什么不对?我以为保护字段和方法是可见的儿童即使后者是在不同的包。

谢谢!


解决方案

  

我想保护的领域和方法
  可见的孩子,即使后者在不同的包中。


这是正确的。类本身必须继承的保护成员的访问。但是,你要做到这一点怎么称呼上的部分的控制参考的getHeight 方法。你可以调用它只是在这个实例!

为了更好的理解,让我引述凯西塞拉利昂的 SCJP preparation指南


  

不过,这是什么意思一个子类外的包有
  访问父(母)的成员?这意味着子类继承
  该成员。没有,但是,平均的
  子类以外的非包可以使用参考访问构件
  超类的一个实例。换句话说,被保护的=
  遗产。 子类可以看到受保护的成员
  只有通过继承


Say there's the following base class:

package bg.svetlin.ui.controls;

public abstract class Control {
    protected int getHeight() {
        //..
    }
    //...
}

Also, in the same package, there's a class that inherits:

package bg.svetlin.ui.controls;

public abstract class LayoutControl extends Control {
    public abstract void addControl(Control control);
    //...
}

Then, there's a third class in another package:

package bg.svetlin.ui.controls.screen;

public abstract class Screen extends LayoutControl {
    //...
}

And, finally, there's the implementation class, again in a different package:

package bg.svetlin.ui.controls.screen.list;    

public class List extends Screen {

    private final Vector controls = new Vector();

    public void addControl(Control control) {
        height += control.getHeight();
        controls.addElement(control);
    }
}

Even though List inherits from Control, and the getHeight() is protected, there's the following error:

getHeight() has protected access in bg.svetlin.ui.controls.Control

I've checked that my imports are right. I'm using NetBeans.

Any idea what's wrong? I thought protected fields and methods are visible to the children even if the latter are in a different package.

Thanks!

解决方案

I thought protected fields and methods are visible to the children even if the latter are in a different package.

That's correct. The class itself has an access to the inherited protected members. But, what you're trying to do it to call the getHeight method on some Control reference. You're allowed to call it only on this instance!

For a better understanding, let me quote Kathy Sierra's SCJP Preparation Guide:

But what does it mean for a subclass-outside-the-package to have access to a superclass (parent) member? It means the subclass inherits the member. It does not, however, mean the subclass-outside-the-package can access the member using a reference to an instance of the superclass. In other words, protected = inheritance. The subclass can see the protected member only through inheritance.

这篇关于为什么我不能调用从继承类另一个包在Java中一个受保护的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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