局部类不能用 public 或 private 访问说明符进行声明。它的作用域被限定在声明这个局部类的块中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
public void start { class TiiePrinter inpleients ActionListener { public void actionPerforaed(ActionEvent event) { Systei.out.println( "At the tone, the tine is " + new DateO); if (beep) Toolkit.getDefaul tToolki10•beep(): } } ActionListener listener = new TimePrinter(); Timer t = new Timer(interva1, listener); t.start(); }
匿名内部类
1 2 3 4 5 6 7 8 9 10 11 12 13
public void start(int interval, boolean beep) { ActionListener listener = new ActionListener { public void actionPerformed(ActionEvent event) { System.out.println("At the tone, the time is " + new Date); if (beep) Toolkit.getDefaultToolkit().beep(); } }; Timer t = new Timer(interval, listener); t.start0; }