9 public class Sum Test public static void
9장. 인터페이스와 예외처리 public class Sum. Test { public static void main(String a 1[]) { int a, b, sum; a = Integer. parse. Int(a 1[0]); b = Integer. parse. Int(a 1[1]); sum = a + b ; // 두 수를 더하는 부분입니다 System. out. println("두수의 "); System. out. println("두수의 합은 " + sum + "입니다"); } } 1
2 인터페이스의 사용 l 클래스에서 인터페이스를 사용하기 위해 implements 예약어를 사용 public interface Sleeper { public long ONE_SECOND = 1000; public long ONE_MINUTE = 60000; public void wakeup(); } public interface Worker { public long WORK_TIME = 8; public void sleep(); } public class Man implements Sleeper, Worker { Man 클래스는 두 개의 인터페이스를 포함하고 있다 public void wakeup() { System. out. println("빨리 일어나 !!"); 인터페이스에서 선언된 메소드는 모두 } 오버라이딩하여 구현하여야 한다. public void sleep() { System. out. println("빨리 자 !!"); . . } } 9장. 인터페이스와 예외처리 6
2 인터페이스의 사용 l 실습예제 Interface. Test 1. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 interface IStack { void push(int item); 두 개의 메소드를 가진 인터페이스 int pop(); } 인터페이스를 포함하는 클래스 class Fixed. Stack implements IStack { private int stack[]; private int tos; Fixed. Stack(int size) { stack = new int[size]; tos = -1; } public void push(int item) { 인터페이스에서 선언된 메소드 오버라이딩 if(tos==stack. length-1) System. out. println("스택이 꽉찼음"); else stack[++tos] = item; } 9장. 인터페이스와 예외처리 7
2 인터페이스의 사용 l 실습예제 Interface. Test 1. java 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 public int pop() { 인터페이스에서 선언된 메소드 오버라이딩 if(tos < 0) { System. out. println("스택이 비었음"); return 0; } else return stack[tos--]; } } public class Interface. Test 1 { public static void main(String args[]) { Fixed. Stack mystack 1 = new Fixed. Stack(10); Fixed. Stack mystack 2 = new Fixed. Stack(5); for(int i=0 ; i<10 ; i++) mystack 1. push(i); for(int i=0 ; i<5 ; i++) mystack 2. push(i); 9장. 인터페이스와 예외처리 8
2 인터페이스의 사용 l 실습예제 Interface. Test 1. java 35 36 37 38 39 40 41 42 43 System. out. println("스택 : mystack 1"); for(int i=0 ; i<10 ; i++) System. out. print(mystack 1. pop() + " "); System. out. println("스택 : mystack 2"); for(int i=0 ; i<5 ; i++) System. out. print(mystack 2. pop() + " "); } } 9장. 인터페이스와 예외처리 9
3 인터페이스 상속 l 인터페이스 선언 시 필요에 따라 다른 인터페이스로부터 상속받을 수 있음 public interface Sleeper { public long ONE_SECOND = 1000; public long ONE_MINUTE = 60000; public void wakeup(); } public interface Worker { public long WORK_TIME = 8; public void sleep(); } public interface People extends Sleeper, Worker { public int MAX = 24; public int MIN = 0; public void work(); } public Interface. Class implements People {. . . public void wakeup() 인터페이스의 모든 메소드가 구현되어야 한다 { } . . . . 구현 public void sleep() { } . . . . 구현 public void worker() { } 인터페이스의 모든 메소드가 구현되어야 한다 . . . . 구현 9장. 인터페이스와 예외처리 12
6 예외 처리 l 입출력을 수행하는 프로그램 import java. io. *; public class IOException. Error { public static void main(String args[]) { File. Reader file = new File. Reader("a. txt"); int i; while((i = file. read()) != -1) { System. out. print((char)i); } file. close(); } } } 9장. 인터페이스와 예외처리 17
6 예외 처리 6 -1 메소드에서 예외 처리 l 실습예제 Exception. Test 1. java 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 import java. io. *; 입출력 패키지를 포함하는 문장(11장 설명) public class Exception. Test 1 { public static void main(String args[]) { try 블록 지정 File. Reader fle = new File. Reader("a. txt"); int i; while((i = fle. read()) != -1) System. out. print((char)i); fle. close(); } 발생된 예외를 처리 catch(Exception e) { System. out. println("예외 처리 루틴 : " + e + " 파일이 존재하지 않는다. "); } } } 9장. 인터페이스와 예외처리 21
6 예외 처리 6 -2 호출한 메소드에 예외를 넘겨주는 방법 l 실습예제 Exception. Test 2. java 01 02 03 04 05 06 07 08 09 10 import java. io. *; public class Exception. Test 2 { public static void main(String args[]) throws Exception { File. Reader fle = new File. Reader("a. txt"); int i; while((i = fle. read()) != -1) System. out. print((char)i); fle. close(); } } 9장. 인터페이스와 예외처리 throws() 절을 이용하여 예외를 JVM에 넘김 24
- Slides: 33