인터페이스 필기
package com.java1.day19;
public class FighterTestEx11 {
public static void main(String[] args) {
Fighter f = new Fighter();
if (f instanceof Unit) {
Unit u = (Unit) f;
System.out.println("f는 Unit클래스의 자손입니다.");
}
if (f instanceof Fightable) {
Fightable f1 = (Fightable) f;
System.out.println("f는 Fightable인터페이스를 구현했습니다.");
}
if (f instanceof Movable) {
Movable m = (Movable) f;
System.out.println("f는 Movable인터페이스를 구현했습니다.");
}
if (f instanceof Attackable) {
Attackable a = (Attackable) f;
System.out.println("f는 Attackable인터페이스를 구현했습니다.");
}
if (f instanceof Object) {
Object obj = (Object) f;
System.out.println("f는 Object클래스의 자손입니다.");
}
}
}
class Fighter extends Unit implements Fightable {
@Override
public void move(int x, int y) {}
@Override
public void attack(Unit u) {}
}
class Unit{
int currentHP;
int x;
int y;
}
interface Fightable extends Movable, Attackable{}
interface Movable {void move(int x, int y);}
interface Attackable {
void attack(Unit u);
static void print(String msg) {
System.out.println("JAVA*");
}
default void defaultTest() {
System.out.println("Default");
}
}
출력결과
f는 Unit클래스의 자손입니다.
f는 Fightable인터페이스를 구현했습니다.
f는 Movable인터페이스를 구현했습니다.
f는 Attackable인터페이스를 구현했습니다.
f는 Object클래스의 자손입니다.