JAVA

190710 다형성 상속 예제

猫猫 2019. 7. 10. 11:05
반응형
package prj190710;

class Tire{
	int maxRotation; //최대회전수
	int acculatedRotation; //누적 회전수
	String location; //타이어 위치
	 
	Tire(){} //초기 생성자
	
	Tire(String location, int maxRotation){ //파라미터 2개 생성자
		this.location = location; //타이어 위치
		this.maxRotation = maxRotation; //최대 회전수 
	}
	
	public boolean roll() { //불린타입 메소드
		++acculatedRotation; //누적회전수 증가
		if(acculatedRotation < maxRotation) {//누적회전수가 최대 회전수보다 작을때 실행
			System.out.println(location+"의 Tire 수명 : "+ (maxRotation-acculatedRotation)+"회");
			return true;
		}
		else {
			System.out.println(location+"Tire 펑크!!!!!"); //누적회전수가 최대회전수보다 크면 펑크
			return false;
		}//if end
	}//method end			
}//class Tire  end

class Car{
	
	Tire FrontLeftTire  = new Tire("앞 왼쪽", 6);
	Tire FrontRightTire = new Tire("앞 오른쪽",2);
	Tire BackLeftTire = new Tire("뒤 왼쪽", 3 );
	Tire BackRightTire = new Tire("뒤 오른쪽", 4);	
	
	int run() {
		System.out.println(">>>>>달리는 중>>>>>");
		if(!FrontLeftTire.roll()) { stop(); return 1;} //Tire클래스 안의 roll메서드가 boolean으로 false 리턴할때 stop메소드와 리턴 실행한다. 
		if(!FrontRightTire.roll()) { stop(); return 2;}
		if(!BackLeftTire.roll()) { stop(); return 3;}
		if(!BackRightTire.roll()) { stop(); return 4;}		
		return 0;
	}//method end
	
	void stop() {
		System.out.println("XXXXX자동차 멈춤XXXXX");
	}//method end
	
}//class Car end


class KoreaTire extends Tire{ //Tire 상속받는 KoreaTire 클래스
	
	KoreaTire(){} //초기생성자
	
	public KoreaTire(String location, int maxRotation) { //파라미터 2개인 생성자
		super(location, maxRotation); //부모클래스(Tire)에게 생성자 정보 두개 넘김.
	}
	
	@Override
	public boolean roll() { //불린타입 메소드
		++acculatedRotation; //누적회전수 증가
		if(acculatedRotation < maxRotation) {//누적회전수가 최대 회전수보다 작을때 실행
			System.out.println(location+"의 KoreaTire 수명 : "+ (maxRotation-acculatedRotation)+"회");
			return true;
		}
		else {
			System.out.println(location+"KoreaTire 펑크!!!!!"); //누적회전수가 최대회전수보다 크면 펑크
			return false;
		}//if end
	}//method end				
}//class KoreaTire end 

class KumhoTire extends Tire{ //Tire 상속받는 KumhoTire 클래스
	
	KumhoTire(){} //초기 생성자
	
	public KumhoTire(String location, int maxRotation) { //파라미터 2개인 생성자
		super(location, maxRotation); //부모클래스 한테 넘김
	}
	
	@Override
	public boolean roll() { //불린타입 메소드
		++acculatedRotation; //누적회전수 증가
		if(acculatedRotation < maxRotation) {//누적회전수가 최대 회전수보다 작을때 실행
			System.out.println(location+"의 KumhoTire 수명 : "+ (maxRotation-acculatedRotation)+"회");
			return true;
		}
		else {
			System.out.println(location+"KumhoTire 펑크!!!!!"); //누적회전수가 최대회전수보다 크면 펑크
			return false;
		}//if end
	}//method end				
}//class KumhoTire end

public class PolyTire {

	public static void main(String[] args) {
		
		Car car = new Car();// Car 객체 생성
		 
		for(int i = 0; i<10; i++) { //5바퀴 회전할것
			
			int problemLocation = car.run(); //car.run 수행하는데(5번), 문제가 생기면 return값 저장할 문제장소 저장.
			
			switch(problemLocation) { //문제장소 저장값을 switch-case로 판단
			case 1: 
				System.out.println("앞 왼쪽 KoreaTire로 교체");
				car.FrontLeftTire = new KoreaTire("앞 왼쪽", 15); 
				//car의 앞왼쪽 타이어 는 이미 car클래스 안에 생성되어있으니 닷 연산자로 호출, 새로운 객체 생성. Tire-KoreaTire는 상속관계이므로
				// 부모참조변수 - 자식 선언 가능
				break;
			case 2:
				System.out.println("앞 오른쪽 KumhoTire로 교체");
				car.FrontRightTire = new KumhoTire("앞 오른쪽", 13);
				break;
			case 3:
				System.out.println("뒤 왼쪽 KoreaTire로 교체");
				car.BackLeftTire = new KoreaTire("뒤 왼쪽", 14);
				break;
			case 4:
				System.out.println("뒤 오른쪽 KumhoTire로 교체");
				car.BackRightTire = new KumhoTire("뒤 오른쪽", 17);
				break;
			}//switch end			
			System.out.println("================"+(i+1)+"회전 end===================");		
		}//for end
		
	}//main end

}//class end

 

 

출처 : https://limkydev.tistory.com/m/75?category=957527

반응형