JAVA
190711 Interface
猫猫
2019. 7. 11. 11:06
반응형
package prj190711;
interface Fightable extends Moveable, Attackable{}
interface Moveable { void move(int x, int y);}
interface Attackable { void attack(UnitX u); }
// Adapter 는 인터페이스와 다르게 모든걸 오버로딩 하는게 아니라 몇개만 골라서 할 수 있다.
// 인터페이슨 강제적으로 구현된 모든것을 오버로딩 해야한다.
class UnitX {
int currentHP;
int x;
int y;
}
class Fighter extends UnitX implements Fightable{
public void move(int x, int y) { }
public void attack(UnitX u) {}
}
public class FighterTest {
public static void main(String[] args) {
Fighter f = new Fighter();
// Fightable g = new Fighter(); //되네?!
// Moveable mo = new Fighter();
// Attackable at = new Fighter();
if( f instanceof UnitX) {
System.out.println("f는 unitX 클래스의 자손");
}
if(f instanceof Fightable) {
System.out.println("f 는 fightable 인터페이스 구현");
}
if(f instanceof Moveable) {
System.out.println("f는 Moveable 인터페이스 구현");
}
if( f instanceof Attackable) {
System.out.println("f는 Attackable 인터페이스 구현");
}
}
}
반응형