package book.ch3.p1;
public class A
 { 
   protected int i;
 }package book.ch3.p2;
import book.ch3.p1.A;
public class B extends A
{
  void test(A a)
  {
    int j=a.i;  //这里为什么会出现错误呀?protected修饰的可以正常访问啊!请求帮助!!!
    int k=i; 
  }
 }

解决方案 »

  1.   


    protected 变量, 是子类和同一个包可见,   void test(A a)
      {
        int j=a.i;  
        int k=i; 
      }
    这里 a 只作为一个参数传入去, 不能算是子类访问。 如果你想在子类访问,可以写成这样:
    package book.ch3.p2;import book.ch3.p1.A;public class B extends A{
    int temp = super.i ; void test(A a) { //    int j=a.i;  
     int k=i;  } public static void main( String [] args ){
    System.out.println( "temp : " + new B().temp ) ;
    }}
      

  2.   

    不在同一个 package 中的类的 protected 成员是无法访问的。
      

  3.   

    这里的protected是指子类 可以访问父类的成员域,而不是指可以访问父类实例的成员域!答案是这样,但我不明白是什么意思啊。求解中ing……
      

  4.   

    orcus(灵月) 正解
    THINKING IN JAVA 里有
      

  5.   

    不在同一个 package 中的类的 protected 成员是无法访问的。
      

  6.   

    看来楼主还没有明白protect的访问原则咯
    orcus(灵月) 正解
      

  7.   

    1 orcus(灵月) 同学说的是friendly也就是默认的访问权限的用法
      gray820928(珏色倾城)犯的也是这个错误
     
    2 protected访问权限是同一个包下的类或者子类可以访问protected修饰的东西;
      正确的解法确实如lgh2008(ar_guang)例子所阐释;  赞一个
      

  8.   

    楼上说得对
    protected只要是子类,在不在一个包,皆可访问这一点在thinking in java 上面恰恰写错了
    很多人也一直认为thinking对,但是thinking还是一本好书
      

  9.   

    Access protected memebers outside the package has two conditions,in The Java Language Specification (3rd Edition),there is a example to explain this:package points;
    public class Point {
            protected int x, y;
            public void move(int dx, int dy) { x += dx; y += dy; }
            public int getX() { return x; }
            public int getY() { return y; }
    }1.The methods move, getX, and getY of the class Point are declared public and so are available to any code that uses an object of type Point.2.The fields x and y are declared protected and are accessible outside the package points only in subclasses of class Point, and only when they are fields of objects that are being implemented by the code that is accessing them.(Note that there are two conditions here).So,assume AnotherPoint is a subclass of Point in another package.If you want to access 
    protected x,and y in Point,you must do this through an object of AnotherPoint,not Point.In your code:
    void test(A a)
    {
    int j=a.i; //1  
    int k=i;   //2
    }line 1 is wrong because you access protected member i through a object of A-a;
    line 2 is right because it equals to "int k=this.i;",and "this" is a reference of an object of B.May this is help for you~~http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.2
      

  10.   

    不在同一个 package 中的类的 protected 成员是无法访问的。
    ------------------------------------------------------
    不是吧,protected修饰符的主要作用是允许其它包中的子类来访问父类的特定习属性.
      

  11.   

    protected 变量可以被继承类访问,或同一包下的其他类访问int j=a.i; //这里不可以访问,因为a.i是不同包下的protected变量
    int k=i; //这可以访问,因为i是继承来的superclass的protected变量
      

  12.   

    这里的protected是指子类 可以访问父类的成员域,而不是指可以访问父类实例的成员域!答案是这样,但我不明白是什么意思啊。求解中ing……
    各位说的我都明白,就是不知道这句话的意思,为什么不能访问父类实例的成员变量呢?
      

  13.   

    这里的protected是指子类 可以访问父类的成员域,而不是指可以访问父类实例的成员域!你写:void test()
       int j=super.i;    看看是不是可以?void test(A a)      //可能相当于a=new A(),就是上面说的父类实例的成员
      

  14.   

    lgh2008(ar_guang) 说得是对的。
    orcus(灵月) 兄弟,TIJ没这样说吧,看书可要仔细一点哦package book.ch3.p1;
    public class A
    {
        protected int i;
    }package book.ch3.p2;
    import book.ch3.p1.A;
    public class B extends A
    {
        void test(A a)
        {
            int j=a.i; //这里不是在子类访问父类protected域,而是直接访问类A的实例域,当然不行了
            int k=i;
        }
        void great()
        {
            i = 100; //这里的i就是继承自父类A了,因为是protected,所以子类可以正常访问
            System.out.println("i == " + i);
        }
    }楼主慎重考虑一下,我也不确定说对了没有
      

  15.   

    事实上,TIJ在讲述这个问题上,确实有点表达上的失误。不过如果你仔细地看过英文版,你可以很容易的理解。
    在第5章讲protected关键字的时候,有这样一句话:
    如果你创建了新的package,并且让你的类继承自另一个package的某个类,那么所有你能访问到的成员只能是声明为public类型的。(当然,如果你在同一个package里继承一个类,你就可以操作所有具有package访问权限的成员了)有时候,基类的创建者可能会想让一个特殊的成员只能被子类访问到,而又不暴露给外部世界。这就是protected所要做的。protected同样给予了package访问权限,也就是说:在同一个package里的其他类也可以访问到protected成员。如果你仔细看书里给出的例子,你就会明白,TIJ并没有表达protected只能被同一个包里的子类访问的意思最后还有一段话,是这样的:
    bite()在dessert包里仍然具有package访问权限,但是它同样可以被任何继承自Cookie的子类访问例子比较长,不好摘录,大概是这样的:
    package c05.dessert;public class Cookie {
        protected void bite() {
            System.out.println("Hello");
        }
    }
    //注意ChocolateChip与Cookie并不是属于同一个包
    import c05.dessert;public class ChocolateChip extends Cookie {
        public static void main(String[] args) {
            ChocolateChip x = new ChocolateChip();
            x.bite();
        }
    }源代码目录结构如下:
    c05
        ----ChocolateChip.java
        ----dessert
                  ----Cookie.java
    晕,我只能解释到这样了,累啊!!!看得明不明白,我也管不了那么多了!最后还要骂一句翻译的人,别误导读者行不行?
      

  16.   

    晕,目录结构被弄乱了
    c05
            dessert(目录)
                    Cookie.java
            ChocolateChip.java
      

  17.   

    靠,怎么搞的
    <pre>
    c05
            ChocolateChip.java
            dessert
                    Cookie.java
    </pre>