运行结果就是:foobarhi
我理解
  (new Bar() {}).go();

  Bar a = new Bar();
  a.go();
是一个意思,不知道是否正确,关注……

解决方案 »

  1.   

    答案是foobarhi
    其实这个题目很简单
    public class Foo {
    Foo() {System.out.print("foo");}
    class Bar{
    Bar() {System.out.print("bar");}
    public void go() {System.out.print("hi");}
    }
    public static void main (String [] args) {
    Foo f = new Foo(); 初始化F,结果应该输出FOO
    f.makeBar();     调用makeBar()函数
    }
    void makeBar() {
    (new Bar() {}).go();初始化函数,输出BAR,再调用GO()输出HAI
    }
    }
      

  2.   

    (new Bar(){}) 与 (new Bar()) 有什么区别?
      

  3.   

    为什么new Bar()后面还有{}呢?调用任何函数时候有这种写的吗? 
    (new Bar()).go();倒好理解。
      

  4.   

    new Bar(){},不妨拆开来看 
    Bar()
    {}
    先声明一个类
    再生成这个类的实例
    new Bar();new Bar()是调用Bar类的构造函数,返回一个Bar实例。
      

  5.   

    其实这样写就可以了:
    new Bar().go();没有必要搞这么多括号,不知这些括号都有什么用?不明的!是不是要弄得很神密的样子才显得java的高深啊?
      

  6.   

    chw_csdn_chw(chw):我觉得你说的有问题,否则的话为什么还会输出bar呢
    我测试了一下,(new Bar(){})中的{}里不能加任何代码,否则就会出现错误,不妨运行一下下面的程序看看:
    public class Foo {
    Foo() {System.out.print("foo");}
    class Bar{
    Bar() {System.out.print("bar");}
    public void go() {System.out.print("hi");}
    }
    public static void main (String [] args) {
    Foo f = new Foo();
    f.makeBar();
    }
    void makeBar() {
    (new Bar() {System.out.print("ha");}).go();
    }
    }
    我运行的时候出现了错误:<identifier> expected,不知道是为什么
      

  7.   

    楼上,考题就这么出的,你要不知道有这种写法,下面的答案选择里,你会不会选编译出错呢?还是知道比较好。what is the result?
    A. Compilation fails.
    B. An error occurs at runtime.
    C. foobarhi
    D. barhi
    E. hi
      

  8.   

    Result is : foobarhi  .
     (new Bar() {}).go() 和 (new Bar()).go() 是一个意思
      

  9.   

    楼上,考题就这么出的,你要不知道有这种写法,下面的答案选择里,你会不会选编译出错呢?还是知道比较好。what is the result?
    A. Compilation fails.
    B. An error occurs at runtime.
    C. foobarhi
    D. barhi
    E. hi
      

  10.   

    其实这题是考你会不会认为编译出错
    new Bar(){}
    这种写法和new Bar()有本质的区别
    new Bar(){}是一个匿名的Bar的子类
    可以在{}之间定义函数,或者覆盖父类函数,这样写就很明白了
    new Bar(){
       public void go() {System.out.print("hihihihi");}
       }
    }
      

  11.   

    其实new Bar() {}确实是一个匿名的内部类,这句话其实是实例化了内部类Bar的一个子类对象,而子类的构造函数会省缺调用父类即Bar的缺省构造函数,因此才会输出bar,
    至于iversonxk的程序出的错误是由于new Bar() {}中{}的内容应该是表示一个子类所拥有的方法和属性的,只有一个System.out当然会出错
      

  12.   

    这是生成了一个匿名的内部类,继承于Bar,毫无疑问
      

  13.   

    to wwwxu888:你自己说的话经过java编译器的验证了吗?
      

  14.   

    完全同意wpx80(wpx80)。如果你写过awt程序的动作监听器actionListener,这个概念很简单
      

  15.   

    还用得着特地去验证?这是很常用的方法,在为组件加ActionListener的时候,无数次用这种匿名内部类了。
    你要看例子,是吧?
    public class Foo {
    Foo() {System.out.print("foo");} class Bar{
    Bar() {System.out.print("bar");}
    public void go() {System.out.print("hi");}
    } public static void main (String [] args) {
    Foo f = new Foo();
    f.makeBar();
    }

    void makeBar() {
    (new Bar() {public void go() {System.out.println("this is what I add!");}}).go();
    }
    }
      

  16.   

    new XXX().a(); ==> XXX x = new XXX(); x.a();(new Bar() {}).go(); ==>Bar bar = new Bar() {};
    bar.go();
      

  17.   

    比较同意wpx80(wpx80) 的看法
    new Bar(){
       super.go();//添加此句更容易理解,执行了Bar的方法
       public void go() {System.out.print("hihihihi");}
       }
    }
      

  18.   

    比较同意wpx80(wpx80) 的看法,它是Bar的匿名子类
    new Bar(){
       public void go() {
        super.go();//添加此句更容易理解,执行了Bar的方法
        System.out.print("hihihihi");}
       }
    }
      

  19.   

    确实是匿名子类,只是它的语法规则比较“奇怪”而已look:
    Anonymous classes can implement an interface (implicit extension of Object) or explicitly extend a class. Cannot do both.
    Syntax: new interfacename() { } or new classname(can take arg.) { }
      

  20.   

    这道题没有变态啊,是正常的语法规则啊匿名子类是没有自己的名字,所以原来类名的那个位置其实就是它extends的那个类的名字或者implements那个接口的名字啊
      

  21.   

    楼上的,不叫重载,叫继承,然后覆盖
    overload和override是不同的,很多人会讲错