-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInnerClassTest.java
More file actions
101 lines (81 loc) · 1.97 KB
/
InnerClassTest.java
File metadata and controls
101 lines (81 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package Chapter6;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;
import javax.swing.JOptionPane;
import javax.swing.Timer;
public class InnerClassTest {
public static void main(String[] args) {
/*TalkingClock tc=new TalkingClock(1000,true);
tc.start();*/
TalkingClock tc=new TalkingClock();
tc.start(1000, true);
JOptionPane.showMessageDialog(null, "Quite");
System.exit(0);
}
}
/*class TalkingClock{
private int interval;
private boolean beep;
public TalkingClock(int interval, boolean beep) {
super();
this.interval = interval;
this.beep = beep;
}
public void start(){
ActionListener listener=new TimePrint();
Timer time=new Timer(interval, listener);
time.start();
}
public class TimePrint implements ActionListener{
public void actionPerformed(ActionEvent e) {
Date now=new Date();
System.out.println("The time is "+now);
if(beep)
Toolkit.getDefaultToolkit().beep();
}
}
}*/
/*
* 局部内部类
*
* 省去多余的变量
* 内部类可以访问局部变量,但是局部变量必须声明为final类型
* 因为TimePrint类在beep释放之前对其做了一个备份
* */
/*class TalkingClock{
public void start(int interval,final boolean beep){
class TimePrint implements ActionListener{
public void actionPerformed(ActionEvent e) {
Date now=new Date();
System.out.println("The time is "+now);
if(beep)
Toolkit.getDefaultToolkit().beep();
}
}
ActionListener listener=new TimePrint();
Timer time=new Timer(interval, listener);
time.start();
}
}*/
/*
*匿名内部类
*
*
*
* */
class TalkingClock{
public void start(int interval,final boolean beep){
ActionListener listener=new ActionListener() {
public void actionPerformed(ActionEvent e) {
Date now=new Date();
System.out.println("The time is "+now);
if(beep)
Toolkit.getDefaultToolkit().beep();
}
};
Timer time=new Timer(interval, listener);
time.start();
}
}