Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- partitioning
- localStorage
- Lag
- frontend
- EventSource
- 실시간알림
- PostgreSQL
- 개발자
- node.js
- SSE
- mariadb
- 파티셔닝
- ServerSentEvent
- node.js란
- Partition
- 성장기
- Node.js기본
- 열공하자
Archives
- Today
- Total
써치킴의 우당탕탕 개발 블로그
[Java][Ch2] 인터페이스 본문
인터페이스란?
-
약속(인터페이스)만 잡고 서로 각자의 부품을 만든다.
-
앞으로 누군가가 구현해야 할 메서드 목록
-
메서드 목록 선언이지 구현은 X -> 구현을 위한 코드블록{}이 포함해서도 안된다.
-
메서드 목록에 속성을 포함해서도 안된다.
-
Exam.java
// 인터페이스는 앞으로 누군가가 구현해야 할 메서드 목록이다.
// (그러므로 메서드 구현을 위한 코드블록{}이 포함해서도 안된다.)
// 또한 메서드 목록에 속성을 포함해서는 안된다.
public interface Exam { // 인터페이스 기준으로 만들어져야함
// interface 내 함수는 public abstract이 무조건 있으므로 public abstract 생략 가능
/*public abstract void input();
public abstract void print();*/
void input(); // 인터페이스를 implement하는 클래스는 반드시 해당함수 포함되어야 한다.
void print(); // 강제화
}
EngExam.java
public class EngExam implements Exam{ // 인터페이스 클래스 사용 시 implements 사용
private int listen;
private int speak;
private int read;
private int write;
public EngExam() {
this(0,0,0,0);
}
public EngExam(int listen, int speak, int read, int write) {
this.listen = listen;
this.speak = speak;
this.read = read;
this.write = write;
}
public int getListen() {
return listen;
}
public void setListen(int listen) {
this.listen = listen;
}
public int getSpeak() {
return speak;
}
public void setSpeak(int speak) {
this.speak = speak;
}
public int getRead() {
return read;
}
public void setRead(int read) {
this.read = read;
}
public int getWrite() {
return write;
}
public void setWrite(int write) {
this.write = write;
}
@Override
public void input() {
Scanner sc = new Scanner(System.in);
int listen;
int speak;
int read;
int write;
System.out.println("-----------------");
System.out.println(" 성적입력");
System.out.println("-----------------");
do{
System.out.println("listen 점수를 입력해주세요.");
listen = sc.nextInt();
if(listen>100 || listen<0){
System.out.println("입력범위를 초과하였습니다. 다시 입력해주세요.");
}
}while(listen>100 || listen<0);
do{
System.out.println("speak 점수를 입력해주세요.");
speak = sc.nextInt();
if(speak>100 || speak<0){
System.out.println("입력범위를 초과하였습니다. 다시 입력해주세요.");
}
}while(speak>100 || speak<0);
do{
System.out.println("read 점수를 입력해주세요.");
read = sc.nextInt();
if(read>100 || read<0){
System.out.println("입력범위를 초과하였습니다. 다시 입력해주세요.");
}
}while(read>100 || read<0);
do{
System.out.println("write 점수를 입력해주세요.");
write = sc.nextInt();
if(write>100 || write<0){
System.out.println("입력범위를 초과하였습니다. 다시 입력해주세요.");
}
}while(write>100 || write<0);
this.listen = listen;
this.speak = speak;
this.read = read;
this.write = write;
}
@Override
public void print() {
int total;
float avg;
System.out.println("-----------------");
System.out.println(" 성적출력");
System.out.println("-----------------");
total = total();
avg = avg();
System.out.println("listen 점수 : " + listen);
System.out.println("speak 점수 : " + speak);
System.out.println("read 점수 : " + read);
System.out.println("write 점수 : " + write);
System.out.println("총점: " + total);
System.out.println("평균: " + avg);
System.out.println();
}
public int total() {
return listen + speak + read + write;
}
private float avg() {
return total()/4.0f;
}
}
MainEx.java
public class MainEx {
public static void main(String[] args) {
Exam exam = new EngExam();
int menu;
esc:
while(true){
menu = inputMenu();
switch (menu) {
case 1:
exam.input();
break;
case 2:
exam.print();
break;
case 3:
System.out.println("성적 프로그램이 종료되었습니다.");
break esc;
default:
System.out.println("1~3번까지의 숫자를 입력해주세요.");
System.out.println();
break;
}
}
}
private static int inputMenu() {
Scanner sc = new Scanner(System.in);
System.out.println("-----------------");
System.out.println(" 메뉴 선택");
System.out.println("-----------------");
System.out.println("1. 성적입력");
System.out.println("2. 성적출력");
System.out.println("3. 종료");
System.out.print("선택 >> ");
int menu = sc.nextInt();
return menu;
}
}
'더 자세하게 Java' 카테고리의 다른 글
[Java][Ch3] Object Class (0) | 2022.05.13 |
---|---|
[Java][Ch2] 내부클래스 (0) | 2022.05.13 |
[Java][Ch2] 추상화(abstract) (0) | 2022.05.13 |
[Java][Ch2] 메서드동적바인딩 (0) | 2022.05.13 |
[Java][Ch2] is-a 상속 (0) | 2022.05.13 |
Comments