본문 바로가기

JAVA

JAVA 필드의 다향성 ( 예제 프로그램 작성

 

다향성이란 동일한 타입을 사용하지만 다양한 결과가 나오는 성질을 말한다.

주로 필드의 값을 다양화함으로써 실행 결과가 다르게 나오도록 구현하는데 필드의 타입은 변환이 없지만 실행도중에

어떤 필드로 저장하느냐에 따라 실행 결과가 달라진다. 이것이 필드의 다향성이다.

 

 

필드의 다향성을 알아볼 프로그램을 작성해보자.

 

 

프로그램설명

1. 자동차 타이어 4개의 수명을 선언

2. 자동차 타이어가 회전 반복

3. 자동차 타이어가 터질 시 다른 타이어로 교체

 

tip : 생성자 자동형변환, 오버로드 사용

 

 

 

 

 

프로그램 실행순서 및 이벤트 설명

1.메인에서 Car의 런메소드 실행
2. Run메소드에서 Tire.roll 실행
3. roll을 실행후 Run메소드에게 트루값 리턴
4. 값이 flase가 될때까지 2번 반복
5. roll 실행후 Run메소드에게 flase 값 리턴
6. Run메소드에서 flase 일경우 return 1
7. int value 값은 Run 리턴값
8. switch 문에서 리턴값의 따른 실행
9. Car메소드의 Tire 생성자를 HankookTire 생성자로 바꾸면서 매개값 수정
10. HankookTire 메소드에서 Tire 상속후 roll 메소드 오버로드(재정의)

Main  //

public class Main {
    public static void main(String[] args){
        
        Car c1 = new Car();
        
       for(int i=1; i < 5; i++){
           int value = c1.run();  
           
           switch(value){
               case 1:
                 System.out.println("[ 앞 왼쪽 타이어 펑크남.]");
                  c1.t1 = new HankookTire("앞 왼쪽", 15);
                   System.out.println("앞 오른쪽 HankookTire로 교체");
                   break;
               case 2:
                 System.out.println("[ 앞 오른쪽 타이어 펑크남.]");
                   c1.t2 = new KumhoTire("앞 오른쪽", 13);
                   System.out.println("앞 오른쪽 KumhoTire로 교체");
                   break;
               case 3:
                 System.out.println("[ 뒤 왼쪽 타이어 펑크 ].");
                   c1.t3 = new HankookTire("뒤 왼쪽", 14);
                   System.out.println("앞 오른쪽 HankookTire로 교체");
                   break;
               case 4:
                 System.out.println("[ 뒤 오른쪽 타이어 펑크남 ].");
                   c1.t4 = new KumhoTire("뒤 오른쪽", 17);
                   System.out.println("앞 오른쪽 KumhoTire로 교체");
                   break;
           }
           System.out.println("----------------------------");
       } 
    }
}

 

 

자동차

public class Car {
    
    Tire t1 = new Tire("앞 왼쪽", 6);
    Tire t2 = new Tire("앞 오른쪽", 2);
    Tire t3 = new Tire("뒤 왼쪽", 3);
    Tire t4 = new Tire("뒤 오른쪽", 4);
    
int run(){
    System.out.println("자동차가 달리기 시작합니다.! ");
    if(t1.roll()==false) { stop(); return 1;}
    if(t2.roll()==false) { stop(); return 2;}
    if(t3.roll()==false) { stop(); return 3;}
    if(t4.roll()==false) { stop(); return 4;}
    
    return 0;  
}

    void stop(){
        System.out.println("자동차 타이어 펑크 발생!");
    }
    
}

 

타이어 

public class Tire{
 
    public int pluas = 0;
    public String location;
    public int max;

    public Tire(String location, int max){
        this.location = location;
        this.max = max;
    }

    public boolean roll(){
        pluas++;
        if(pluas < max){
    
            System.out.println(location+" 타이어 아직 살아있음!  남은 수명 : "+(max-pluas));
            return true;
        }else{
            System.out.println("*****"+location+" 타이거 펑크남***********");
            return false;
        }
    }
}

 

교체할 타이어 HankookTire

public class HankookTire extends Tire {
    
    public HankookTire(String location, int max){
        super(location, max); // 바로위 부모 객체 필드의 접근 가능 
    }
    
    public boolean roll(){
         pluas++;
        if(pluas < max){
            System.out.println(location+"타이어 아직 살아있음! 남은 수명 : "+(max-pluas));
            return true;
        }else{
            System.out.println("*****"+location+" 타이거 펑크남***********");
            return false;
        }
    }
}

 

교체할 타이어 kumhoTire

public class KumhoTire extends Tire{
    
    public KumhoTire(String location, int max){
        super(location, max);
    }
    public boolean roll(){
         pluas++;
        if(pluas < max){
            System.out.println(location+"타이어 아직 살아있음! 남은 수명 : "+(max-pluas));
            return true;
        }else{
            System.out.println("*****"+location+" 타이거 펑크남***********");
            return false;
        }
    }
 
}