这是关于这种类的资料:
Anonymous Classes
You can declare an inner class without naming it. Here's yet another version of the now-tired Stack class, in this case using an anonymous class for its enumerator:
public class Stack {
private Vector items; ...//code for Stack's methods and constructors not shown... public Enumeration enumerator() {
return new Enumeration() {
int currentItem = items.size() - 1;
public boolean hasMoreElements() {
return (currentItem >= 0);
}
public Object nextElement() {
if (!hasMoreElements())
throw new NoSuchElementException();
else
return items.elementAt(currentItem--);
}
}
}
}Anonymous classes can make code difficult to read. You should limit their use to those classes that are very small (no more than a method or two) and whose use is well-understood (like the AWT event-handling adapter classes).我的理解是相当于:
WindowAdapter st = new WindowAdapter();
...addWindowListener (st);class WindowAdapter ( )
{
public void windowClosing ( WindowEvent e )
{
System.exit ( 0 ) ;
}
}
Anonymous Classes
You can declare an inner class without naming it. Here's yet another version of the now-tired Stack class, in this case using an anonymous class for its enumerator:
public class Stack {
private Vector items; ...//code for Stack's methods and constructors not shown... public Enumeration enumerator() {
return new Enumeration() {
int currentItem = items.size() - 1;
public boolean hasMoreElements() {
return (currentItem >= 0);
}
public Object nextElement() {
if (!hasMoreElements())
throw new NoSuchElementException();
else
return items.elementAt(currentItem--);
}
}
}
}Anonymous classes can make code difficult to read. You should limit their use to those classes that are very small (no more than a method or two) and whose use is well-understood (like the AWT event-handling adapter classes).我的理解是相当于:
WindowAdapter st = new WindowAdapter();
...addWindowListener (st);class WindowAdapter ( )
{
public void windowClosing ( WindowEvent e )
{
System.exit ( 0 ) ;
}
}
解决方案 »
- 问大家关于java的IO流这边的几个问题,希望能得到你们的答案,谢谢了!
- 在线等待急,请问我安装的数据库是SqlServer express版,Java程序中连接数据应用什么驱动程序啊?
- lookupPrintServices(null, null) 找不到打印服务!!急!!!
- 关于用Swing实现类似Office中注释文本框效果
- 向高手请教输入输出问题!
- 如何有鼠标事件获得JButton所在的JFrame? 急
- check the best java forum I've ever seen!!!
- 请问在linux下用什么编译器?linux自带了JAVA的编译器吗?
- OA系统文件上传
- 新手 关于冒泡排序的问题
- 有applet的网页需要什么样的环境才能运行?
- 大家帮个忙,俺的作业不会做了。。。谢谢了,
new XXX(){......}其实就是
class ThisClassDozntHaveAName extends/implemnts XXX{
public ThisClassDozntHaveAName(){
.....
}
.....
}extends/implements 取决于XXXX 是class 还是interface,
注意匿名类只能用一个interface,因为你不可能 new A, B, C(){...}
也不能 new ABC() implements/extends ANOTHER