인터페이스(Interface)
- 기본 형태 : 접근제어자, 반환 타입, 메소드 이름만을 정의한다.
interface myInterface {
void myMethod(int x); // 함수(중괄호) 구현 불가
}
- 인터페이스에 함수 내용(중괄호) 구현은 불가능하다.
- 인터페이스를 사용하는 클래스는 인터페이스의 중괄호 안의 함수를 반드시 구현해야한다.
- 모두 구현을 한다면 여러 개의 인터페이스를 생성할 수 있다.
interface Flyable {
void fly(int x, int y, int z);
}
class Pigeon implements Flyable {
// 인터페이스를 implement 하려면 Flyable 함수 내용을 구현해야 함
private int x,y,z;
@Override
public void fly(int x, int y, int z) {
printlocation();
System.out.println("날아갑니다.");
this.x = x;
this.y = y;
this.z = z;
printlocation();
}
public void printlocation(){
System.out.println("현재 위치 {"+x+", "+y+","+z+"}");
}
}
public class Main {
public static void main(String[] args) {
Flyable pigeon = new Pigeon();
pigeon.fly(1,2,3);
}
}