실습(부모클래스 호출 복습 - 동물 키우기)
![](https://blog.kakaocdn.net/dn/bw1MQg/btrrJuAnigc/hQ6VtVA9ZHK5yfoVV6oUS1/img.png)
※ sleep 메소드 test
public class SleepTest {
public static void main(String[] args) {
System.out.print("시작");
for(int i = 0; i < 5; i++) {
System.out.print("~");
try {
Thread.sleep(1000); // 5초 멈춤
} catch (InterruptedException e) {
System.out.println("오류 발생 시 오는 영역");
}
}
System.out.println("종료");
}
}
![](https://blog.kakaocdn.net/dn/bp5BTi/btrrD7l9rGC/mMQEaIoHbPHGERkfG3VT8k/img.png)
1. 부모 클래스(Animal.java) 생성
public class Animal {
int hp; // 체력을 저장할 변수
int feed; // 음식 개수를 저장할 변수
public Animal(int hp, int feed) {
this.hp = hp;
this.feed = feed;
}
public void show() {
System.out.println("현재 체력 : " + this.hp);
System.out.println("남은 먹이 : " + this.feed);
System.out.println("--------------------");
}
// 먹이주기
public void eat() {
this.hp++;
this.feed--;
show();
}
// 산책가기
public void walk() {
this.hp--;
this.feed++;
show();
}
// 씻기기
public void wash() {
this.hp += 20;
this.feed += 20;
System.out.print("씻는 중");
for(int i = 0; i < 1; i++) {
System.out.print("~");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(); // 줄바꿈
show();
}
}
2. 자식 클래스(Dog.java) 생성
public class Dog extends Animal{
public Dog(int hp, int feed) {
super(hp, feed);
}
// 먹이주기
@Override
public void eat() {
this.hp += 2;
this.feed -= 5;
show();
}
// 산책가기
@Override
public void walk() {
this.hp -= 2;
super.walk(); // hp--, feed++, show()
}
// 씻기기
@Override
public void wash() {
this.hp += 20;
this.feed += 20;
System.out.print("강아지 씻는중");
for(int i = 0; i < 3; i++) {
System.out.print("~강아지");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println();
show();
}
}
3. 자식 클래스(Monkey.java) 생성
public class Monkey extends Animal{
public Monkey(int hp, int feed) {
super(hp, feed);
}
// 먹이주기
@Override
public void eat() {
this.hp += 2;
super.eat(); // hp++, feed--, show()
}
// 산책가기
@Override
public void walk() {
this.hp -= 4;
this.feed += 2;
super.walk(); // hp--, feed++, show()
}
// 씻기기
@Override
public void wash() {
this.hp += 20;
this.feed += 20;
System.out.print("원숭이 씻는중");
for(int i = 0; i < 10; i++) {
System.out.print("~원숭이");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println();
show();
}
}
4. 자식 클래스(Iguana.java) 생성
public class Iguana extends Animal{
public Iguana(int hp, int feed) {
super(hp, feed);
}
// 먹이주기 : 부모에 정의된 메소드 사용
// 산책가기 : 부모에 정의된 메소드 사용
// 씻기기
@Override
public void wash() {
this.hp += 20;
this.feed += 20;
System.out.print("이구아나 씻는중");
for(int i = 0; i < 5; i++) {
System.out.print("~이구아나");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println();
show();
}
public void onlyIguana() {
System.out.println("이구아나 종료");
}
}
5. Main.java에서 실행
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = 0;
Animal animal = null;
while(true) {
System.out.println("-----캐릭터 선택-----");
System.out.println("1. 강아지");
System.out.println("2. 원숭이");
System.out.println("3. 이구아나");
System.out.print("입력 >> ");
choice = sc.nextInt();
if(choice == 1) {
animal = new Dog(50, 50);
break;
} else if(choice == 2) {
animal = new Monkey(20, 20);
break;
} else if(choice == 3) {
animal = new Iguana(5, 5);
break;
} else {
System.out.println("잘못 입력했습니다");
}
}
while(true) {
System.out.println("1. 먹이주기 2. 산책가기 3. 씻기기 4. 종료하기");
System.out.print("입력 >> ");
choice = sc.nextInt();
if(choice == 4) {
break;
} else if(choice == 1) {
animal.eat();
} else if(choice == 2) {
animal.walk();
} else if(choice == 3) {
animal.wash();
} else {
System.out.println("잘못 입력했습니다");
}
}
// animal.onlyIguana(); // 업캐스팅되면 자식에서만 새롭게 정의된 메소드를 사용할 수 없다.
if(animal instanceof Iguana) {
Iguana i = (Iguana)animal; // 다운캐스팅
i.onlyIguana();
}
}
}
![](https://blog.kakaocdn.net/dn/OFJ0b/btrrJu8dW5N/GNY0YMa9kYzFeEmND8wB61/img.png)
![](https://blog.kakaocdn.net/dn/xnwHy/btrrIfqeHa2/K4XjE3ZL2bhpHmIw7BGwk0/img.png)
![](https://blog.kakaocdn.net/dn/WGbwl/btrrIfjpk4B/Jkq2SrddhDAoPpMMp8VNhK/img.png)