参考文章:http://www.cnblogs.com/nerxious/archive/2013/01/25/2876489.html
匿名内部类(anonymous inner class):
顾名思义,没有名字的内部类。表面上看起来它们似乎有名字,实际那不是它们的名字。
匿名内部类就是没有名字的内部类。什么情况下需要使用匿名内部类?如果满足下面的一些条件,使用匿名内部类是比较合适的:
·只用到类的一个实例。
·类在定义后马上用到。
·类非常小(SUN推荐是在4行代码以下)
·给类命名并不会导致你的代码更容易被理解。
在使用匿名内部类时,要记住以下几个原则:
·匿名内部类不能有构造方法。
·匿名内部类不能定义任何静态成员、方法和类。
·匿名内部类不能是public,protected,private,static。
·只能创建匿名内部类的一个实例。
·一个匿名内部类一定是在new的后面,用其隐含实现一个接口或实现一个类。
·因匿名内部类为局部内部类,所以局部内部类的所有限制都对其生效。
匿名内部类也就是没有名字的内部类
正因为没有名字,所以匿名内部类只能使用一次,它通常用来简化代码编写
但使用匿名内部类还有个前提条件:必须继承一个父类或实现一个接口
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
abstract class Person { public abstract void eat(); } class Child extends Person { public void eat() { System.out.println( "eat something" ); } } public class Demo { public static void main(String[] args) { Person p = new Child(); p.eat(); } } |
运行结果:eat something
可以看到,我们用Child继承了Person类,然后实现了Child的一个实例,将其向上转型为Person类的引用
但是,如果此处的Child类只使用一次,那么将其编写为独立的一个类岂不是很麻烦?
这个时候就引入了匿名内部类
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
abstract class Person { public abstract void eat(); } public class Demo { public static void main(String[] args) { Person p = new Person() { public void eat() { System.out.println( "eat something" ); } }; p.eat(); } } |
运行结果:eat something
可以看到,我们直接将抽象类Person中的方法在大括号中实现了
这样便可以省略一个类的书写
并且,匿名内部类还能用于接口上
interface Person { public void eat(); } public class Demo { public static void main(String[] args) { Person p = new Person() { public void eat() { System.out.println( "eat something" ); } }; p.eat(); } } |
运行结果:eat something
由上面的例子可以看出,只要一个类是抽象的或是一个接口,那么其子类中的方法都可以使用匿名内部类来实现
最常用的情况就是在多线程的实现上,因为要实现多线程必须继承Thread类或是继承Runnable接口
public class Demo { public static void main(String[] args) { Thread t = new Thread() { public void run() { for ( int i = 1 ; i <= 5 ; i++) { System.out.print(i + " " ); } } }; t.start(); } } |
运行结果:1 2 3 4 5
1
2
3
4
5
6
7
8
9
10
11
12
13
|
public class Demo { public static void main(String[] args) { Runnable r = new Runnable() { public void run() { for ( int i = 1 ; i <= 5 ; i++) { System.out.print(i + " " ); } } }; Thread t = new Thread(r); t.start(); } } |
运行结果:1 2 3 4 5
public class AnonymousInnerClassTest {
public static void main(String[] args) {
TalkingClock clock = new TalkingClock();
clock.start(1000,true);
JOptionPane.showMessageDialog(null,"Quit program?");
System.exit(0);
}
}
1 public class AnonymousInnerClassTest { 2 public static void main(String[] args) { 3 TalkingClock clock = new TalkingClock(); 4 clock.start(1000,true); 5 JOptionPane.showMessageDialog(null,"Quit program?"); 6 System.exit(0); 7 } 8 } 9 class TalkingClock{ 10 public void start(int interval,final boolean beep){ 11 ActionListener listener = new ActionListener() { 12 @Override 13 public void actionPerformed(ActionEvent e) { 14 Date now = new Date(); 15 System.out.println("At the tone,the time is " + now); 16 if (beep){ 17 Toolkit.getDefaultToolkit().beep(); 18 } 19 } 20 }; 21 Timer t = new Timer(interval,listener); 22 t.start(); 23 } 24 }
原文:http://www.cnblogs.com/levelhi/p/6403785.html