匿名内部类错误,抄书的,肯定是小错误,大神们看看,指教下!!!

bh1990 2014-05-10 01:40:06
package test;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.Timer;

public class test01 {
public static void main(String[] args){

}
}
class TalkingClock1{
public void start(int interval,final boolean beep){
ActionListener listener=new ActionListener();//Cannot instantiate the type ActionListener
{
public void actionPerformed(ActionEvent e){//void is an invalid type for the variable actionPerformed
Date now=new Date();
System.out.println("At this tone,the time is "+now);
if(beep)Toolkit.getDefaultToolkit().beep();
}
}
Timer t=new Timer(interval,listener);
t.start();
}
}
特别要说说怎么改,是放在新的package里的!
...全文
124 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
痞子2号 2014-05-11
  • 打赏
  • 举报
回复
嗯!知道了,下午调试了!改过来了!
河滩上的蝎子 2014-05-10
  • 打赏
  • 举报
回复
引用 2 楼 yangzongbin 的回复:
public void actionPerformed(ActionEvent e) 这个方法不能嵌套使用,即不能放在别的方法中! ActionListener listener=new ActionListener();至于这个抽象类不能实例化,我也没找到解决方法!
这是一个匿名的内部类的定义,你的理解完全偏了方向。
河滩上的蝎子 2014-05-10
  • 打赏
  • 举报
回复
看这一句:
ActionListener listener = new ActionListener();
直接new 一个接口,编译器肯定报错。 匿名类的定义格式是这样的:
Test t = new T(){//要定义的内部类};
而不是这样的:
Test t = new T();{//要定义的内部类}
痞子2号 2014-05-10
  • 打赏
  • 举报
回复
为什么去了ActionListener listener=new ActionListener();后面的分号,提示还是有错?
-江沐风- 2014-05-10
  • 打赏
  • 举报
回复
看3喽
-江沐风- 2014-05-10
  • 打赏
  • 举报
回复
发帖时引用一下好点
xuefeng0707 2014-05-10
  • 打赏
  • 举报
回复
ActionListener listener=new ActionListener();
这一行应该没分号。
痞子2号 2014-05-10
  • 打赏
  • 举报
回复
public void actionPerformed(ActionEvent e) 这个方法不能嵌套使用,即不能放在别的方法中! ActionListener listener=new ActionListener();至于这个抽象类不能实例化,我也没找到解决方法!
bh1990 2014-05-10
  • 打赏
  • 举报
回复
用下划线标出来了,和书里是一模一样的,Eclipse提示错误如注释所述!

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧