MainClass02, Weapon, Gun




MainClass02.useWeapon(new Gun());
static으로 선언했기 때문에 객체생성 없이 useWeapon을 호출할 수 있다.
또 같은 클래스 안에 있기 때문에 MainClass02는 생략 가능하다. (예시에서는 생략하지 않았다.)
public static void useWeapon(Weapon w)
useWeapon이 인자로 weapon type을 요구한다. main 메소드에서 useWeapon 메소드를 실행할때 weapon type 인자가 아닌 Gun type 인자를 전달했음에도 정상 작동하는 이유는 Gun class에서 Weapon을 상속(extends)받기 때문이다.
console 창에 w.attack() 의 결과로 shot이 출력되는 이유는 Gun class에서 attack 메소드를 재정의(override) 했기 때문이다.
'java' 카테고리의 다른 글
| Step10_Interface(interface, final, 익명클래스) (0) | 2022.07.29 |
|---|---|
| Step09_AbstractClass(inner class, 익명클래스) (0) | 2022.07.29 |
| Step09_AbstractClass(추상클래스, 추상메소드) (0) | 2022.07.29 |
| eclipse로 git 사용하기2(gitignore) (0) | 2022.07.29 |
| Step08_Extends(protected, 객체인자) (0) | 2022.07.28 |