答案是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 } }
(new Bar(){}) 与 (new Bar()) 有什么区别?
为什么new Bar()后面还有{}呢?调用任何函数时候有这种写的吗? (new Bar()).go();倒好理解。
new Bar(){},不妨拆开来看 Bar() {} 先声明一个类 再生成这个类的实例 new Bar();new Bar()是调用Bar类的构造函数,返回一个Bar实例。
其实这样写就可以了: new Bar().go();没有必要搞这么多括号,不知这些括号都有什么用?不明的!是不是要弄得很神密的样子才显得java的高深啊?
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,不知道是为什么
楼上,考题就这么出的,你要不知道有这种写法,下面的答案选择里,你会不会选编译出错呢?还是知道比较好。what is the result? A. Compilation fails. B. An error occurs at runtime. C. foobarhi D. barhi E. hi
Result is : foobarhi . (new Bar() {}).go() 和 (new Bar()).go() 是一个意思
楼上,考题就这么出的,你要不知道有这种写法,下面的答案选择里,你会不会选编译出错呢?还是知道比较好。what is the result? A. Compilation fails. B. An error occurs at runtime. C. foobarhi D. barhi E. hi
其实这题是考你会不会认为编译出错 new Bar(){} 这种写法和new Bar()有本质的区别 new Bar(){}是一个匿名的Bar的子类 可以在{}之间定义函数,或者覆盖父类函数,这样写就很明白了 new Bar(){ public void go() {System.out.print("hihihihi");} } }
还用得着特地去验证?这是很常用的方法,在为组件加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(); } }
new XXX().a(); ==> XXX x = new XXX(); x.a();(new Bar() {}).go(); ==>Bar bar = new Bar() {}; bar.go();
比较同意wpx80(wpx80) 的看法 new Bar(){ super.go();//添加此句更容易理解,执行了Bar的方法 public void go() {System.out.print("hihihihi");} } }
比较同意wpx80(wpx80) 的看法,它是Bar的匿名子类 new Bar(){ public void go() { super.go();//添加此句更容易理解,执行了Bar的方法 System.out.print("hihihihi");} } }
确实是匿名子类,只是它的语法规则比较“奇怪”而已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.) { }
其实这个题目很简单
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
}
}
(new Bar()).go();倒好理解。
Bar()
{}
先声明一个类
再生成这个类的实例
new Bar();new Bar()是调用Bar类的构造函数,返回一个Bar实例。
new Bar().go();没有必要搞这么多括号,不知这些括号都有什么用?不明的!是不是要弄得很神密的样子才显得java的高深啊?
我测试了一下,(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,不知道是为什么
A. Compilation fails.
B. An error occurs at runtime.
C. foobarhi
D. barhi
E. hi
(new Bar() {}).go() 和 (new Bar()).go() 是一个意思
A. Compilation fails.
B. An error occurs at runtime.
C. foobarhi
D. barhi
E. hi
new Bar(){}
这种写法和new Bar()有本质的区别
new Bar(){}是一个匿名的Bar的子类
可以在{}之间定义函数,或者覆盖父类函数,这样写就很明白了
new Bar(){
public void go() {System.out.print("hihihihi");}
}
}
至于iversonxk的程序出的错误是由于new Bar() {}中{}的内容应该是表示一个子类所拥有的方法和属性的,只有一个System.out当然会出错
你要看例子,是吧?
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();
}
}
bar.go();
new Bar(){
super.go();//添加此句更容易理解,执行了Bar的方法
public void go() {System.out.print("hihihihi");}
}
}
new Bar(){
public void go() {
super.go();//添加此句更容易理解,执行了Bar的方法
System.out.print("hihihihi");}
}
}
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.) { }
overload和override是不同的,很多人会讲错