반응형
인터페이스
인터페이스 설명 보러가기
https://skylarcoding.tistory.com/253
자바 객체지향 - 인터페이스 implements
인터페이스추상 메서드의 집합구현된 것이 전혀 없는 설계도이다. 껍데기 (모든 멤버가 public) interface 인터페이스이름 { public static final 타입 상수이름 = 값 ; public abstract 메서드이름 (매개변수목
skylarcoding.tistory.com
다형성
다형성 설명 보러가기
https://skylarcoding.tistory.com/251
자바 객체지향 - 다형성
다형성이란조상 타입 참조 변수로 자손 타입 객체를 다루는 것 * 자손 타입의 참조변수로 조상 타입의 객체를 가리킬 수 없다. 있는 기능을 안 쓰는 건 괜찮은데, 실제 가지고 있는 기능보다 버
skylarcoding.tistory.com
인터페이스와 다형성
오버라이딩 규칙은 조상(public) 보다 접근 범위가 넓어야 한다.
class Fighter 은 인터페이스 Fightable 을 implements (구현) 했기 때문에 Fightable 로 반환할 수 있다.
class Fighter extends Unit2 implements Fightable {
public void move(int x, int y){
System.out.println("[" + x + "," + y + "]로 이동");
}
public void attack(Fightable f){
System.out.println(f+"를 공격");
}
// 싸울 수 있는 상대를 불러온다.
Fightable getFightable(){
Fightable f = new Fighter(); // Fighter를 생성해서 반환
return f;
}
}
public class FighterTest(){
public static void main(String[] args){
Fighter f = new Fighter();
Fightable f2 = f.getFIghtable();
}
}반응형