相关疑难解决方法(0)

受保护的成员访问java中不同的包 - 一个好奇心

package packageOne;
public class Base
{
    protected void display(){
        System.out.println("in Base");
    }
}


package packageTwo;
public class Derived extends packageOne.Base {
    public void show(){
        new Base().display(); //this is not working throws compilation error that display() from the type Base is not visible
        new Derived().display(); //is working
        display(); //is working
    }
}
Run Code Online (Sandbox Code Playgroud)

这两个包有两个不同的文件.但为什么会这样呢?

java protected

14
推荐指数
2
解决办法
2万
查看次数

标签 统计

java ×1

protected ×1