日期:2014-05-20  浏览次数:20657 次

爪哇的。。。。内部类。。。。
先上段代码。。。
Java code

abstract class Contents{
  abstract public int value();
}

interface Destination{
    String readLable();
}
public class Parcel3 {
  private class PContents extends Contents{
      private int i = 11;
      public int value(){
          return i;
      }
  }
  protected class PDestination implements Destination {
      private String label;
      private PDestination(String whereTo){
          label = whereTo;
      }
      public String readLabel(){
          return label;    
      }
  }
  public Destination dest(String s){
      return new PDestination(s);
  }
  public Contents cont(){
      return new PContents();
  }
}

class Testsss {
    public static void main(String[] args){
        Parcel3 p = new Parcel3();
        Contents c = p.cont();
        Destination d = p.dest("Tanzania");
        Parcel3.PDestination s = new PDestination();
    }
}


问:【1】最后一句的Parcel3.PDestination s = new PDestination();无法编译 PDestination();报错 ,为什么会这样呢?PDestination内部类不是protected吗 ? 那么包类的其他类也可以访问啊 左边的Parcel3.PDestination都可以 ,为什么右边Parcel3.PDestination这样的话却不行呢? 难道不能用new创建PDestination对象?为什么?
  【2】内部类到底是怎么回事啊 我在看Java编程思想 感觉说的不清不楚的 【3】 还有访问到底是什么意思呢?看到内部类这 突然感觉自己连如何判断访问性都不懂。。。。迷糊了。。。

------解决方案--------------------
Parcel3 p = new Parcel3();
Parcel3.PDestination s = p.new PDestination();

没啥好搞的...写法规则而已...
------解决方案--------------------
内部类只有父类可以调用吧~
------解决方案--------------------
看看如何实例化内部类你的问题就迎刃而解了
------解决方案--------------------
new PDestination();你的内部类没有这样的构造器,要写成这样:p.new PDestination(“数据”);
------解决方案--------------------
探讨

Parcel3.PDestination s = new PDestination();
……