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

初学没看懂这段代码的意思
浏览网页的时候看到介绍JAVA内部类,页面上给出如下代码,不过我没太看懂它的意思,为什么这么写?main方法里的写法也不太懂。我是初学,我会自己看一些别的书来弄懂的,不过也想听听大家是如何理解的。

public   interface   Contents   {
  int   value();
  }
  
  public   interface   Destination   {
  String   readLabel();
  }
  
  public   class   Goods   {
  private   class   Content   implements   Contents   {
  private   int   i   =   11;
  public   int   value()   {
  return   i;
  }
  }
  
  protected   class   GDestination   implements   Destination   {
  private   String   label;
  private   GDestination(String   whereTo)   {
  label   =   whereTo;
  }
  public   String   readLabel()   {
  return   label;
  }
  }
  
  public   Destination   dest(String   s)   {
  return   new   GDestination(s);
  }
  public   Contents   cont()   {
  return   new   Content();
  }
  }
  
  class   TestGoods   {
  public   static   void   main(String[]   args)   {
  Goods   p   =   new   Goods();
  Contents   c   =   p.cont();
  Destination   d   =   p.dest( "Beijing ");
  }
  }


------解决方案--------------------
内部类要运行起来的话必须要先执行外部类的构造函数。先实例化外部类在实例化内部类。之后在一次调用方法和过程。你你问题指的是什么不懂?
------解决方案--------------------
public Destination dest(String s) {
  return new GDestination(s);
}
public Contents cont() {
  return new Content();
}

以上的这段代码是什么意思?有点迷糊~

------解决方案--------------------

public Destination dest(String s) {
  return new GDestination(s);
} //返回一个GDestination类型的对象;在你所给的程序里似乎是通过dest方法生成下一个目的地的对象

public Contents cont() {
  return new Content();
} //返回一个Content类型的对象

------解决方案--------------------
我认为这就是内部类的一个用法,用一个类的方法调用内部类生成内部类对象,其他就应该没什么可说的了
------解决方案--------------------
Content 程序里有这个类吗???
如果要是Contents接口的话。?
public Contents cont() {
  return new Contents();
  }
怎么可能给接口实例化??
------解决方案--------------------
Goods类中的第一行定义的就是Content这个类.