관리 메뉴

βœπŸ»κΈ°λ‘ν•˜λŠ” λΈ”λ‘œκ·Έ

[ JAVA ] λ‹€ν˜•μ„± polymorphism λ³Έλ¬Έ

Language/Java

[ JAVA ] λ‹€ν˜•μ„± polymorphism

μ©μ‹œν‚΄ 2020. 12. 6. 20:35
728x90

λ‹€ν˜•μ„± (polymorphism)

ν•˜λ‚˜μ˜ 객체가 μ—¬λŸ¬ 가지 νƒ€μž…μ„ κ°€μ§ˆ 수 μžˆλŠ” 것

 

μ΅œμƒμœ„ 클래슀 Animal을 상속받은 ν•˜μœ„ 클래슀 Human, Tiger, Eagleλ₯Ό μ •μ˜ν•œλ‹€.

Human, Tiger, Eagle 각각의 ν΄λž˜μŠ€λŠ” λΆ€λͺ¨ ν΄λž˜μŠ€μ—μ„œ μ •μ˜ν•œ move λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜ν•œλ‹€. (λ©”μ„œλ“œ μ˜€λ²„ 라이딩)

Humanν΄λž˜μŠ€μ—μ„œλ§Œ μ •μ˜λœ λ©”μ„œλ“œ readBooksκ°€ μ‘΄μž¬ν•œλ‹€.

package polymorphism;

import java.util.ArrayList;

class Animal {
    public void move() {
        System.out.println("동물이 μ›€μ§μž…λ‹ˆλ‹€.");
    }
}

class Human extends Animal{
    public void move(){
        System.out.println("μ‚¬λžŒμ΄ λ‘λ°œλ‘œ κ±·μŠ΅λ‹ˆλ‹€.");
    }

    public void readBooks(){
        System.out.println("μ‚¬λžŒμ΄ 책을 μ½μŠ΅λ‹ˆλ‹€.");
    }
}

class Tiger extends Animal{
    public void move(){
        System.out.println("ν˜Έλž‘μ΄κ°€ λ„€λ°œλ‘œ λœλ‹ˆλ‹€.");
    }
}

class Eagle extends Animal{
    public void move(){
        System.out.println("λ…μˆ˜λ¦¬κ°€ ν•˜λŠ˜μ„ λ‚ μ•„κ°‘λ‹ˆλ‹€.");
    }
}

 

λ‹€ν˜•μ„±μ„ ν™•μΈν•΄λ³΄μž.

상속받은 λΆ€λͺ¨ 클래슀 νƒ€μž…μœΌλ‘œ μΈμŠ€ν„΄μŠ€ 생성

Human, Tiger, Eagle이 각각의 ν΄λž˜μŠ€μ΄μ§€λ§Œ 같은 μžλ£Œν˜• Animal을 가진닀. κ·ΈλŸ¬λ―€λ‘œ hAnimal, tAnimal, eAnimal듀은 상속받은 Animal 클래슀λ₯Ό λ‚΄ν¬ν•œλ‹€.

Animal hAnimal = new Human();//upcasting  
Animal tAnimal = new Tiger();
Animal eAnimal = new Eagle();
        

AnimalTest test = new AnimalTest();
test.moveAnimal(hAnimal);
test.moveAnimal(tAnimal);
test.moveAnimal(eAnimal);

μ˜€λ²„ λΌμ΄λ”©λ˜μ—ˆμ„ λ•Œ λ©”μ„œλ“œμ˜ ν˜ΈμΆœμ€ μΈμŠ€ν„΄μŠ€μ˜ λ©”μ„œλ“œκ°€ 호좜됨
각각의 ν΄λž˜μŠ€μ— λ™μΌν•œ κΈ°λŠ₯의 λ©”μ„œλ“œλ₯Ό κ΅¬ν˜„ν•˜μ—¬ ν˜ΈμΆœν•  수 μžˆμ§€λ§Œ μžλ°”μ˜ λ‹€ν˜•μ„±μ„ μ΄μš©ν•˜λ©΄ ν•œ 번만 μ„ μ–Έν•˜μ—¬ μ‚¬μš©μ΄ κ°€λŠ₯

 

Human ν΄λž˜μŠ€μ— μƒμ„±ν•œ readBooks λ©”μ„œλ“œλŠ” 호좜이 λΆˆκ°€ν•˜λ‹€.

readBooks λ©”μ†Œλ“œλŠ” Humanν΄λž˜μŠ€μ—μ„œλ§Œ μƒμ„±λœ 것이지 상속받은 Animal ν΄λž˜μŠ€μ—μ„œ μƒμ„±λœκ²ƒμ΄ μ•„λ‹ˆκΈ° λ•Œλ¬Έ

hAnimal.readBooks();

 

μ‹€ν–‰κ²°κ³Ό

 Animalνƒ€μž…μ˜ move λ©”μ„œλ“œκ°€ μ•„λ‹Œ 각 μΈμŠ€ν„΄μŠ€μ˜ λ©”μ†Œλ“œ μ‹€ν–‰λœλ‹€. 
 (가상 λ©”μ„œλ“œμ— μ˜ν•΄  λ§€ν•‘λ˜λŠ” ν•¨μˆ˜κ°€ 호좜 )

μžλ°”μ˜ λ‹€ν˜•μ„±μ΄λΌλŠ” νŠΉμ§•μœΌλ‘œ 인해 μ½”λ“œλŠ” ν•˜λ‚˜μ§€λ§Œ κ΅¬ν˜„λœ λ‚΄μš©μ΄ λ‹€λ₯΄κ²Œ λ‚˜μ˜¨λ‹€.

μ‹€ν–‰μ½”λ“œ

public class AnimalTest {
    public static void main(String[] args) {
        Animal hAnimal = new Human();//upcasting
        Animal tAnimal = new Tiger();
        Animal eAnimal = new Eagle();
       
        AnimalTest test = new AnimalTest();
        test.moveAnimal(hAnimal);
        test.moveAnimal(tAnimal);
        test.moveAnimal(eAnimal);

        //hAnimal.readBooks();
       
        ArrayList<Animal> animalArrayList = new ArrayList<Animal>();
        animalArrayList.add(hAnimal);
        animalArrayList.add(tAnimal);
        animalArrayList.add(eAnimal);

        for (Animal animal : animalArrayList){
            animal.move();
        }

    }

    public void moveAnimal(Animal animal) {
        animal.move();//ν•˜λ‚˜μ˜ μ½”λ“œ μ—¬λŸ¬ μžλ£Œν˜• μ‹€ν–‰
    }

    //λ§Œμ•½ 각각의 객체에 λ©”μ†Œλ“œλ₯Ό λ§Œλ“ λ‹€λ©΄ μ˜€λ²„λ‘œλ”© λ°œμƒ
    public void moveAnimal(Human animal) {
        animal.move();
    }
}

 

728x90
λ°˜μ‘ν˜•