Java中受保护的引用
这个问题在这里已经有了答案:
  protected成员只能通过继承在包外的子类中访问。  试试这个: 
public class C extends A {
    void foo() {
       int b = i;  
    }
}
每次都没有必要参考。 我认为你不了解遗传。
public class B extends A {
    void foo() {
       // A a = new A(); No need to instantiate it here as B extends A
        int b = i;  //No nedd to refer it through a.a.i  
    }
}
package another.pac;
public class C extends A {
    void foo() {
        C c=new C();
        int d=c.i//this will work fine
       //   A a = new A(); same as above explanation
        int b = i;  //same as above and even a.i will not compile
    }
}
现在您的受保护的变量将在这里访问..
A类是包装袋的一部分;
而C类是另一个包package.pac,因此它将无法访问其成员。 如果C是包pac的一部分,那么它将能够访问该成员
请参阅以下文章:在Java中,默认,公共,受保护和私有之间的区别
链接地址: http://www.djcxy.com/p/24075.html上一篇: protected references in Java
下一篇: I can access a protected field outside of class in java
