Java核心技术_笔记6-8

1.局部内部类中,若只创建一个类的对象(只用了一次),就不用命名了,即 匿名内部类
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.start 0 ;
}
形式即
new SuperType ( construction parameters )
{
inner class methods and data
}

2.匿名类没有名字,所以没有构造函数
用构造器参数传递给超类来代替
类对象:
Person queen = new Person ( " Mary " ) ;
// a Person object
扩展了此类的匿名类对象
Person count = new Person ( " Dracula " ) { . . .);
/ / an object of an inner class extending Person

3.匿名子类
invite(new ArrayList< String>0{{add(“Harry”);add(“Tony”);}})
第一个{}是创建ArrayList的匿名子类
第二个{}是构造块

4.静态内部类
没有对生成它的 外围类 的引用特权
可拥有自己的静态域和方法
声明在接口中的内部类 自动变为static和public

5.代理proxy
代理可以在运行时创建一个实现了一组给定了接口 的新类

猜你喜欢

转载自blog.csdn.net/Matrix576/article/details/82749970