익명 클래스(anonymous class)
new 조상 클래스 이름() {
// 멤버 선언
}
new 구현 인터페이스 이름() {
// 멤버 선언
}
ex7_17
class Ex7_17 {
Object iv = new Object(){ void method(){} }; // 익명 클래스
static Object cv = new Object(){ void method(){} }; // 익명 클래스
void myMethod() {
Object lv = new Object(){ void method(){} }; // 익명 클래스
}
}
ex7_18
import java.awt.*;
import java.awt.event.*;
class Ex7_18 {
public static void main(String[] args) {
Button b = new Button("Start");
b.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent occurred!!!");
}
});
}
}
class EventHandler implements ActionListener { //이걸 익명클래스로 바꿔서 쓴 게 위에 코
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent occurred!!!");
}
}