Notice
Recent Posts
Recent Comments
Link
«   2025/08   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
Tags
more
Archives
Today
Total
관리 메뉴

노승현

포켓몬 상속으로 객체 만들기 본문

JAVA/문제풀이

포켓몬 상속으로 객체 만들기

nsh95 2023. 12. 1. 20:12
package class04;

class Pokemon{
	String type;
	int lv;
	int exp;
	String name;
	Pokemon(String type){	// 객체를 만들 마음 x / type 멤버변수 값을 강제하기 위함
		this.type=type;
		this.lv = 5;
		this.exp=0;
		this.name="포켓몬";
	}
	void attack() {
		System.out.println("로그 남기기");
		System.out.println("포켓몬 클래스의 attack()");
		System.out.println("아직 구현되지 않았습니다.");
	}
	void hello() {
		System.out.println("로그 남기기");
		System.out.println("포켓몬 클래스의 hello()");
		System.out.println("아직 구현되지 않았습니다.");
	}
}
class Elec extends Pokemon{

	Elec() {
		super("전기타입");
	}

	@Override	// 어노테이션 : 코드 가독성 향상, 메모리 성능 향상, 설정 사항
	void attack() {
		System.out.println("백만볼트~~~~~");
	}
	
	
}
class Water extends Pokemon{
	Water(){
		super("물타입");
	}

	@Override
	void attack() {
		System.out.println("물대포----------");
	}
	
}

class Squirtle extends Water{
	Squirtle(){
		this("꼬부기");
	}
	Squirtle(String name){
		this.name=name;
		
	}
	@Override
	void hello() {
		System.out.println("꼬북꼬북");
	}
}
class Pikachu extends Elec{
	Pikachu(){
		this("피카츄");
	}
	Pikachu(String name){
		this.name=name;
	}
	@Override
	void hello() {
		System.out.println("피카피카");
	}
}

public class Test02 {
	public static void main(String[] args) {
		
		
		Pikachu pika = new Pikachu();
		Squirtle sq = new Squirtle();
		pika.attack();
		pika.hello();
		
		sq.attack();
		sq.hello();
		
		// 가장 최근에 정의된(오버라이딩된) 메서드가 자동호출됨
		// == 다형성이 구현되었다.
		
		// 클래스는 자료형
		
		Pikachu[] pikas = new Pikachu[6];
		// 피카츄만 6마리
		
		Pokemon[] pokemons= new Pokemon[6];
		// 여러종류의 포켓몬 6마리
		pokemons[0] =new Pikachu();
		
		
		
		
		
		
		
		
	}
}

'JAVA > 문제풀이' 카테고리의 다른 글

추상 클래스와 인터페이스 사용한 티비 동작  (0) 2023.12.06
포켓몬스터 복습  (1) 2023.12.02
선택정렬(selection sort)  (0) 2023.11.29
삽입정렬 코드 및 디버깅표  (0) 2023.11.28
버블정렬  (0) 2023.11.27