티스토리 뷰

728x90
반응형

1) 의미 : 이름이 없는 일회용 클래스. 

2) 문법 

3) 특징

- 정의와 생성을 동시에 함

- 조상클래스(Object) 이름 or 인터페이스 이름 + () { 클래스 내용 }

ex.예시

public class Ex7_18 {
	public static void main(String[] args) {
		Button b = new Button("Start");
		b.addActionListener(new EventHandler());
	}
}
class EventHandler implements ActionListener{ //조상클래스 명 : ActionListener
	public void actionPerformed(ActionEvent e) {
		System.out.println("ActionEvent occurred!!!");
	}
}

위 예시를 익명 클래스로 바꿔보기

public 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!!!");
			}
		});
	}
}

 

 

 

익명 클래스 설명이 잘 되어 있는 좋은 사이트 : 

https://limkydev.tistory.com/226

728x90
반응형

'Java > JAVA의 정석_객체지향' 카테고리의 다른 글

내부 클래스(inner class)  (0) 2022.11.11
default 메서드  (0) 2022.11.11
인터페이스를 이용한 다형성  (0) 2022.11.02
인터페이스(interface)  (0) 2022.11.02
추상 클래스(abstract class)  (0) 2022.11.02
댓글
250x250
반응형
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday