on
[JAVA 기초] 상속과 다형성의 간단한 예제 (동물)
[JAVA 기초] 상속과 다형성의 간단한 예제 (동물)
강의를 참고해 만든 프로그램이므로 코딩 난이도는 다소 떨어질 수 있습니다.
Introduction
ArrayList를 이용해 동물 클래스로부터 상속받는 Human, Tiger, Eagle, Bear 클래스를 만든다.
각 클래스는 공통적으로 move()메서드가 있으며, 각각 다른 기능의 메서드가 존재한다.
구현은 print문을 통해 간단히 확인한다.
부모 클래스 (class Animal)
class Animal { public void move() { System.out.println("동물이 움직입니다."); } }
큰 카테고리인 동물 클래스를 만들고 이로부터 상속받을 수 있도록 코드를 구현한다.
자식 클래스 (class Human, Tiger, Eagle, Bear)
class Human extends Animal { public void move() { System.out.println("사람이 두 발로 걷습니다."); } public void readBook() // 사람만이 할 수 있는 메서드 { System.out.println("사람이 책을 읽습니다."); } } class Tiger extends Animal { public void move() { System.out.println("호랑이가 네 발로 뜁니다."); } public void Hunting() { System.out.println("호랑이가 사냥을 합니다."); } } class Eagle extends Animal { public void move() { System.out.println("독수리가 하늘을 납니다."); } public void flying() { System.out.println("독수리가 날고 있습니다."); } } class Bear extends Animal { public void move() { System.out.println("곰이 이동합니다."); } public void biting() { System.out.println("곰이 베어뭅니다."); } }
각 메서드별로 다른 출력문을 구현한다.
메인 클래스 (public class AnimalTest)
public class AnimalTest { public static void main(String[] args) { AnimalTest test = new AnimalTest(); test.moveAnimal(new Human()); test.moveAnimal(new Tiger()); test.moveAnimal(new Eagle()); test.moveAnimal(new Bear()); Animal[] animalList = new Animal[4]; animalList[0] = new Human(); ArrayList animals = new ArrayList(); } public void moveAnimal(Animal animal) // 공통 기능인 moveAnimal { animal.move(); // 이렇게만 적어주어도 human, tiger, eagle, bear에 대한 move를 출력할 수 있으므로 // 이 코드 한 줄이 다형성을 나타낸다고 할 수 있다. // if문을 통해 다운캐스팅된 animal의 메서드가 Human일 경우 human의 readBook() 메서드를 수행하도록 지정. if (animal instanceof Human) // instanceof 를 통해 다운캐스팅을 해준다. { Human human = (Human)animal; human.readBook(); } else if (animal instanceof Tiger) { Tiger tiger = (Tiger)animal; tiger.Hunting(); } else if (animal instanceof Eagle) { Eagle eagle = (Eagle)animal; eagle.flying(); } else if (animal instanceof Bear) { Bear bear = (Bear)animal; bear.biting(); } else { System.out.println("지원되지 않는 기능입니다."); // 예외 지정 } } }
메인에서 다운캐스팅 여부 확인.
전체 코드 (AnimalTest.java)
package chapter8_witharraylist; import java.util.ArrayList; import chapter8_inheritance.Animal; class Animal { public void move() { System.out.println("동물이 움직입니다."); } } class Human extends Animal { public void move() { System.out.println("사람이 두 발로 걷습니다."); } public void readBook() // 사람만이 할 수 있는 메서드 { System.out.println("사람이 책을 읽습니다."); } } class Tiger extends Animal { public void move() { System.out.println("호랑이가 네 발로 뜁니다."); } public void Hunting() { System.out.println("호랑이가 사냥을 합니다."); } } class Eagle extends Animal { public void move() { System.out.println("독수리가 하늘을 납니다."); } public void flying() { System.out.println("독수리가 날고 있습니다."); } } class Bear extends Animal { public void move() { System.out.println("곰이 이동합니다."); } public void biting() { System.out.println("곰이 베어뭅니다."); } } public class AnimalTest { public static void main(String[] args) { AnimalTest test = new AnimalTest(); test.moveAnimal(new Human()); test.moveAnimal(new Tiger()); test.moveAnimal(new Eagle()); test.moveAnimal(new Bear()); Animal[] animalList = new Animal[4]; animalList[0] = new Human(); ArrayList animals = new ArrayList(); } public void moveAnimal(Animal animal) // 공통 기능인 moveAnimal { animal.move(); // 이렇게만 적어주어도 human, tiger, eagle, bear에 대한 move를 출력할 수 있으므로 // 이 코드 한 줄이 다형성을 나타낸다고 할 수 있다. // if문을 통해 다운캐스팅된 animal의 메서드가 Human일 경우 human의 readBook() 메서드를 수행하도록 지정. if (animal instanceof Human) // instanceof 를 통해 다운캐스팅을 해준다. { Human human = (Human)animal; human.readBook(); } else if (animal instanceof Tiger) { Tiger tiger = (Tiger)animal; tiger.Hunting(); } else if (animal instanceof Eagle) { Eagle eagle = (Eagle)animal; eagle.flying(); } else if (animal instanceof Bear) { Bear bear = (Bear)animal; bear.biting(); } else { System.out.println("지원되지 않는 기능입니다."); // 예외 지정 } } }
상속과 다형성을 간단한 예제를 통해 구현하였다.
이 코드에서는 Animal이라는 상위 클래스에서 상속받는 Human, Tiger, Eagle, Bear 클래스가 존재한다.
animal 관련 코드 한 줄로도 각각 메서드별 출력이 될 수 있는지 "다형성"을 확인할 수 있는 코드이다.
Result
from http://lefton.tistory.com/15 by ccl(A) rewrite - 2021-12-25 18:01:36