국비교육 18일차 Java : Final, Interface, Exception
public final class Final_test { }
과 같이 클래스에도 사용될 수 있는데, final로 되어진 클래스는 부모 클래스가 될 수 없다. 클래스에서 implements(구현) 해주게 되면 인터페이스에 있는 추상 메소드를 반드시 클래스에서 사용해주어야 한다.
인터페이스
public interface Inter_example {
double ex(double a); // 인터페이스에 생성되는 메소드는 미완성 메소드
}
public class Example implements Inter_example {
@Override
// 인터페이스에 있는 미완성 메소드는 반드시 클래스에서 재정의 되어야 한다.
public double ex(double a) {
return 0;
}
}
public static void main(String[] args) {
// 클래스로 인스턴스 받아오기
Example ep = new Example();
System.out.println(ep.ex(1)); // 0.0
// 인터페이스로 인스턴스 받아오기
Inter_example ep1 = new Example();
System.out.println(ep1.ex(1)); // 0.0
/*
>>> 다형성(Polymorphism) <<<
인터페이스로 구현된 클래스의 객체는 그 인터페이스의 타입으로 받을 수 있다는 것이 다형성(Polymorphism)이다.
인스턴스를 만들 때는, 인터페이스를 더 자주 사용하게 되는데
왜냐하면 인터페이스는 목차같은 역할이기 때문에 인스턴스의 메소드들이 다 각 클래스 안에 있기 때문이다.
인스턴스를 받아오는 것은 인터페이스이지만 실제로 작동하는 것은 각 클래스에서 오버라이딩 된 메소드들이다.
*/
// 배열로 인스턴스 받아오기
Inter_example[] ex_arr = new Inter_example[3];
// 객체를 만든 것이 아니라 배열을 통해 저장할 공간을 만들어 준 것.
ex_arr[0] = new Example();
// 여기에는 Example 클래스 뿐만 아니라
// Inter_example이 구현되어 있는 모든 클래스가 다 들어올 수 있다.
} // end of main
public static void main(String[] args) {
// 방법1
int[] num_arr = {10,20,30};
for(int i=3; i>=0; i--) {
try {
int val = num_arr[i]/i;
System.out.println(val);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(">> 배열의 인덱스 범위가 초과되었습니다. <<");
} catch (ArithmeticException e) {
System.out.println(">> 분모에는 0 이 올 수 없습니다. <<\n");
}
} // end of for
///////////////////////////////////////////
// 방법2
int[] num2_arr = {10,20,30};
for(int i=3; i>=0; i--) {
try {
int val = num2_arr[i]/i;
System.out.println(val);
} catch (ArrayIndexOutOfBoundsException | ArithmeticException e) {
e.printStackTrace();
}
} // end of for
} // end of main
public static void main(String[] args) {
String[] str_arr = {"10","스물","30"};
for(int i=3; i>=0; i--) { // 3,2,1,0 4번 반복
try {
int val = Integer.parseInt(str_arr[i])/i;
// 문자열을 숫자로 돌려서 나누기
System.out.println(val);
} catch (ArrayIndexOutOfBoundsException e) {
// 만약 ArrayIndexOutOfBoundsException 이 오류라면 여기
System.out.println(">> 배열의 인덱스 범위가 초과되었습니다. <<");
} catch (ArithmeticException e) {
// 만약 ArithmeticException 이 오류라면 여기
System.out.println(">> 분모에는 0 이 올 수 없습니다. <<\n");
} catch(Exception e) {
// 나머지 모든 오류라면 여기(if-else문 같은 느낌)
// Exception은 나머지 모든 오류를 다 받기 때문에 가장 마지막에 와야한다.
System.out.println("오류 메시지 : " + e.getMessage());
}
} // end of for
} // end of main
public static void main(String[] args) {
String[] str_num_arr = {"10","20","삼십","40"};
int count = 0;
for( String str : str_num_arr) {
try {
System.out.println(Integer.parseInt(str));
} catch(Exception e) { // 오류가 나게 되면 catch에서 받음
System.out.println(str+"(은)는 숫자로 변경이 불가합니다.");
} finally {
// finally는 위에서 오류가 발생하든지
// 또는 위에서 오류가 발생하지 않든지 관계없이
// 무조건 실행해야 하는 것들은 finally 속에 기술해주면 된다.
System.out.println(++count
+ "번 반복 >> 여기는 오류가 발생하든 발생하지 않든 실행되어야 합니다. << \n");
}
} // end of for
} // end of main
e.getMessage()
: 오류 메시지를 알려주는 것
e.printStackTrace()
: 어디가 오류인지 모여주는 것
public class Example_exception extends Exception {
private static final long serialVersionUID = 1L;
// 기본 생성자
public Example_exception() {
// super(" 하고싶은 말 작성(이게 오류 메시지) ");
// 부모클래스의 생성자 super();
super(">> 5보다 작은 수는 입력 불가능 합니다. <<\n");
}
// 파라미터가 있는 생성자
public Example_exception(String err_msg) {
super(err_msg);
// 다른 클래스에서 호출해서 에러 메시지를 내가 하고싶은 대로 적을 수 있다.
}
} // end of class
public class Example {
// field
int input_number;
// method
// getter-setter
public int getNumber() {
return input_number;
}
public void setNumber(int number) {
this.input_number = number;
}
// 받아온 숫자를 출력해주는 메소드
public void num_info() {
System.out.println("입력한 숫자 : " + input_number);
}
// 5보다 큰 값만 받아와야하는 메소드 1
public void num_five(int input_num) throws Example_exception {
// num_five라는 메소드를 실행했을 때
// 5보다 작은 값이 들어오게 되면 오류를 출력
if(input_num < 5) {
throw new Example_exception();
// 만일 input_num의 값이 5보다 작다면 Example_exception 실행시킨다.
// 그래서 객체를 하나 만들어서 만일 input_num의 값이 5보다 작다면
// Example_exception 예외를 던지게 된다.
} else {
System.out.println("입력한 값은 " + input_num + "입니다. \n");
}
} // end of void num_five
// 5보다 큰 값만 받아와야하는 메소드 2
public void num_five2(int input_num) throws Example_exception {
if(input_num < 5) {
throw new Example_exception("5보다 작은 수인 "+ input_num + "은 입력이 안된다!!!!!!!");
// Example_exception 클래스의 파라미터가 있는 생성자를 받아온 것.
} else {
System.out.println("입력한 값은 " + input_num + "입니다. \n");
}
} // end of void num_five
// 입력한 값을 보여주면서 5보다 큰 값만 받아와야하는 메소드
public void num_info_five(int input_num) throws Example_exception {
num_five2(input_num);
// throws Example_exception를 적어주기 전에는 에러가 발생함
// 현재 클래스에서 try-catch로 에러를 해결해주어도 괜찮으나,
// 'add throws declaration'을 눌러서 throws Example_exception 추가해주면
// 메인 클래스에서 에러 메시지 작성을 할 수 있게 된다.
num_info();
}
}
public static void main(String[] args) {
Example ex = new Example();
ex.input_number = 3;
ex.num_info();
// 입력한 숫자 : 3
// 5보다 큰 값만 받아와야하는 메소드 1 출력
try {
ex.num_five(ex.input_number);
} catch (Example_exception e) {
System.out.println("오류메시지 : " + e.getMessage());
//오류메시지 : >> 5보다 작은 수는 입력 불가능 합니다. <<
}
// 5보다 큰 값만 받아와야하는 메소드 2 출력
try {
ex.num_five2(ex.input_number);
} catch (Example_exception e) {
System.out.println("오류메시지 : " + e.getMessage());
// 오류메시지 : 5보다 작은 수인 4은 입력이 안된다!!!!!!!
}
// 입력한 값을 보여주면서 5보다 큰 값만 받아와야하는 메소드 출력
try {
ex.num_info_five(ex.input_number);
} catch (Example_exception e) {
System.out.println(ex.input_number + "(은)는 5보다 작은 수이니 다시 입력하세요. \n");
// Example 클래스에서 메인클래스로 넘긴 예외 메시지를 여기서 입력해줄 수 있음.
// 입력한 숫자 : 3
// 3(은)는 5보다 작은 수이니 다시 입력하세요.
}
} // end of main
사용자(개발자)가 정의하는 예외(exception)절 클래스 여기는 내가 간략하게 정리하느라고 만들어낸 코드라서 오류 있을지도 모름... 허허....