티스토리 뷰
내부 클래스
- outter class의 멤버를 자유롭게 씀 -> 확장
- static 필드를 쓸 목적
1. member inner class
public class MemberInner { //초기화 시켜주면 안되지만 확인을 위해 초기화
private int a = 10;
public int b = 20;
protected static int c = 30;
class Member{ //Inner class
public void disp() {
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
public static void main(String[] args) {
//MemberInner mi = new MemberInner();
//MemberInner.Member m = mi.new Member();
MemberInner.Member m = new MemberInner().new Member();
m.disp();
}
}
2. static inner class : 외부 마음대로 접근 못함
public class MemberInner {
private int a = 10;
public int b = 20;
protected static int c = 30;
static class Member{ //Static Inner class
public void disp() {
//System.out.println(a);
//System.out.println(b);
System.out.println(c);
}
}
public static void main(String[] args) {
MemberInner.Member m = new MemberInner.Member();
m.disp();
}
}
3. local inner class : 클래스 안에 있는 특정 메소드 안에 클래스를 생성 할 수 있다
public class MemberInner {
private int a = 10;
public int b = 20;
protected static int c = 30;
public void output() {
class Member { // Local Inner Class
public void disp() {
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
}
//output()안에 있는 Member 클래스 선언
Member m = new Member();
m.disp();
}
public static void main(String[] args) {
MemberInner mi = new MemberInner();
mi.output();
}
}
4. anonymous inner class : 익명 - 1회용 ex) 이벤트 처리
abstract class Abs {
abstract void disp();
}
public class MemberInner {
private int a = 10;
public int b = 20;
protected static int c = 30;
public Abs abs = new Abs() {
@Override
void disp() {
System.out.println(a);
System.out.println(b);
System.out.println(c);
}
};
public static void main(String[] args) {
MemberInner mi = new MemberInner();
mi.abs.disp();
}
}
예시
import java.awt.*;
import java.awt.event.*;
class WindowHandler extends WindowAdapter {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
}
class KeyEventHandler extends KeyAdapter {
//member inner class
TextField tf;
TextArea ta;
public KeyEventHandler(TextField tf, TextArea ta) {
//call by address로 연결
this.tf = tf;
this.ta = ta;
}
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
ta.append(tf.getText() + "\n");
tf.setText("");
}
}
}
public class AnonymousEx extends Frame implements ActionListener {
//anonymous inner class
Panel p1, p2, p3;
TextField tf;
TextArea ta;
Button b1, b2;
public AnonymousEx() {
super("Adapter 테스트");
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
tf = new TextField(35);
ta = new TextArea(10, 35);
b1 = new Button("Clear");
b2 = new Button("Exit");
p1.add(tf);
p2.add(ta);
p3.add(b1);
p3.add(b2);
add("North", p1);
add("Center", p2);
add("South", p3);
setBounds(300, 200, 300, 300);
setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
tf.addKeyListener(new KeyEventHandler(tf, ta));
addWindowListener(new WindowHandler());
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("Clear")) {
ta.setText("");
tf.setText("");
tf.requestFocus();
}
else if (str.equals("Exit")) {
System.exit(0);
}
}
public static void main(String[] args) {
AnonymousEx ae = new AnonymousEx();
}
}
위의 코드을 anonymous inner class를 사용하여 구현
import java.awt.*;
import java.awt.event.*;
public class AnonymousInner extends Frame implements ActionListener {
Panel p1, p2, p3;
TextField tf;
TextArea ta;
Button b1, b2;
public AnonymousInner() {
super("Adapter 테스트");
p1 = new Panel();
p2 = new Panel();
p3 = new Panel();
tf = new TextField(35);
ta = new TextArea(10, 35);
b1 = new Button("Clear");
b2 = new Button("Exit");
p1.add(tf);
p2.add(ta);
p3.add(b1);
p3.add(b2);
add("North", p1);
add("Center", p2);
add("South", p3);
setBounds(300, 200, 300, 300);
setVisible(true);
b1.addActionListener(this);
b2.addActionListener(this);
// anonymous inner class - keyEventHandler
tf.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == KeyEvent.VK_ENTER) {
ta.append(tf.getText() + "\n");
tf.setText("");
}
}
});
// anonymous inner class - windowHandler
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) { // 재정의해서 알아서 부르게끔 해줌
System.exit(0);
}
});
}
public void actionPerformed(ActionEvent e) {
String str = e.getActionCommand();
if (str.equals("Clear")) {
ta.setText("");
tf.setText("");
tf.requestFocus();
} else if (str.equals("Exit")) {
System.exit(0);
}
}
//keyEventHandler 원래 코드
/*
* class KeyEventHandler extends KeyAdapter { //ta, tf를 직접 접근 가능 public void
* keyTyped(KeyEvent e) { if (e.getKeyChar() == KeyEvent.VK_ENTER) {
* ta.append(tf.getText() + "\n"); tf.setText(""); } } }
*/
// Member inner class - keyEventHandler
/*
* class KeyEventHandler extends KeyAdapter { public void keyTyped(KeyEvent e) {
* if (e.getKeyChar() == KeyEvent.VK_ENTER) { ta.append(tf.getText() + "\n");
* tf.setText(""); } } }
*/
public static void main(String[] args) {
new AnonymousInner();
}
}
'LANGUAGE > JAVA' 카테고리의 다른 글
[JAVA] SYNCHRONIZED - 동기화 (0) | 2018.03.27 |
---|---|
[JAVA] GUI (0) | 2018.03.26 |
[JAVA] INTERFACE - 인터페이스 (0) | 2018.03.22 |
[JAVA] STACK / QUEUE (0) | 2018.03.21 |
[JAVA] CLASS - 클래스 (0) | 2018.03.21 |
댓글