先说结论

匿名内部类分两种,一种是接口的匿名实现,一种是类的匿名子类!后者往往用于修改特定方法。

再说起因

本来以为匿名内部类很简单,就是接口的匿名实现,直到我发现了下面这段代码:

 1 public class FooBeanInfo extends SimpleBeanInfo {
 2 
 3     public PropertyDescriptor[] getPropertyDescriptors() {
 4         try {
 5             final PropertyEditor numberPE = new CustomNumberEditor(Integer.class, true);
 6             PropertyDescriptor ageDescriptor = new PropertyDescriptor("age", Foo.class) {
 7                 public PropertyEditor createPropertyEditor(Object bean) {
 8                     return numberPE;
 9                 };
10             };
11             return new PropertyDescriptor[] { ageDescriptor };
12         }
13         catch (IntrospectionException ex) {
14             throw new Error(ex.toString());
15         }
16     }
17 }
View Code

相关文章: