JAVA/예제

[JAVA] ch07-20. 객체 지향 20

밍글링글링 2017. 8. 22.
728x90
public class Ex20 {
    public static void main(String[] args){
        Fighter fighter = new Fighter();

        if(fighter instanceof Thing) //fighter는 Thing에 소속되어있다.. (true)이므로 출력.
            System.out.println("fight instanceof Thing");
        if(fighter instanceof Fightable) //fighter is instance of Fightable.
            System.out.println("fight instanceof Fightable");
        if(fighter instanceof Movable)
            System.out.println("fight instanceof Movable");
        if(fighter instanceof Attackable)
            System.out.println("fight instanceof Attackable");
        if(fighter instanceof Object)
            System.out.println("fight instanceof Object");
    }
}

class Thing{
    int hp;
    int x;
    int y;
}

interface  Movable{
    void move(int x, int y);
}

interface Attackable{
    void attack(Thing thing); //선언부만 사용
}

interface Fightable extends Movable, Attackable{}
    
class Fighter extends Thing implements Fightable{
    public void move(int x, int y) {}
    public void attack(Thing thing) {}
}
 

728x90

'JAVA > 예제' 카테고리의 다른 글

[JAVA] ch08-01. 예외 처리  (0) 2017.08.22
[JAVA] ch07-21. 팩토리 패턴(Factory Pattern)  (0) 2017.08.22
[JAVA] ch07-19. 객체 지향 19  (0) 2017.08.22
[JAVA] ch07-18. 객체 지향 18  (0) 2017.08.22
[JAVA] ch07-17. 객체 지향 17  (0) 2017.08.22

댓글