第一问题出在
     private  Bar  b;  
第二问题表现了多态性

解决方案 »

  1.   

    在这里,this可以指向该类的一个对象的地址,或者在某些情况下代表指向该行语句所在类的指针。我觉得这里是后者。
    你把Bar改成方法后,实际上在FooTool中覆盖了Foo2中的Bar方法。如不用super指明那么,FooTool就会调用你新定义的方法,所以第二次输出了Footoo.bar。
    在第一次的代码中,FooToo1的Bar类是一个类种类,始终没有被实例化,new  FooToo1();这一句使Foo2实例化,同时,b=this.new  Bar();也使Foo2中的Bar实例化。我也是初学,解释不当之处,多包涵。
      

  2.   

    takecare(大厅) 
    我觉得和private  Bar  b;  没什么关系,private虽然不能继承,但在这里只是为了防止b在子类中被修改,b的使用还使没什么问题的,好像private不足以解释第一次输出的结果吧
      

  3.   

    呵呵,终于在《thinking in java》中找到了答案:
    What happens when you create an inner class, then inherit from the enclosing class and redefine the inner class? That is, is it possible to override an inner class? This seems like it would be a powerful concept, but “overriding” an inner class as if it were another method of the outer class doesn’t really do anything:
    //: c08:BigEgg.java
    // An inner class cannot be overriden 
    // like a method.class Egg {
      protected class Yolk {
        public Yolk() {
          System.out.println("Egg.Yolk()");
        }
      }
      private Yolk y;
      public Egg() {
        System.out.println("New Egg()");
        y = new Yolk();
      }
    }public class BigEgg extends Egg {
      public class Yolk {
        public Yolk() {
          System.out.println("BigEgg.Yolk()");
        }
      }
      public static void main(String[] args) {
        new BigEgg();
      }
    } ///:~
    The default constructor is synthesized automatically by the compiler, and this calls the base-class default constructor. You might think that since a BigEgg is being created, the “overridden” version of Yolk would be used, but this is not the case. The output is:  
    New Egg()
    Egg.Yolk()This example simply shows that there isn’t any extra inner class magic going on when you inherit from the outer class. The two inner classes are completely separate entities, each in their own namespace. However, it’s still possible to explicitly inherit from the inner class。也就是说程序里的Foo2中的内嵌类Bar实际上被赋予的名字是Foo2$Bar,而FooTool的内嵌类Bar被赋予的名字是FooTool$Bar,因为内嵌类是不能被覆写的,所以实际上在FooTool里同时存在Foo2$Bar和FooTool$Bar两个类,b=this.new Bar()此时的this指针实际上就是指向的是刚产生的FooTool对象,而b=this.new Bar()这句因为存在于Foo2类中,所以实际是b=this.new Foo2$Bar();所以输出的是Foo2.Bar.
      

  4.   

    Foo2$Bar,FooTool$Bar是在编译之后产生独立文件,Foo2$Bar.class  FooTool$Bar.class,看样子你是弄明白了:)