기록장

필드의 다형성 코드 짜본거 본문

개발/JAVA

필드의 다형성 코드 짜본거

HJJJJJ 2022. 5. 21. 17:16
728x90
package com.day3;

public class KumhoTire extends Tire {
	//필드
	//생성자
	public KumhoTire(String location, int maxRotation) {
		super(location, maxRotation);
	}
	//메소드
	@Override
	public boolean roll() {
		++accumulatedRotstion;
		if(accumulatedRotstion<maxRotation) {
			System.out.println(location+ "KumhoTire 수명"+
		(maxRotation-accumulatedRotstion) + "회");
			return true;
		}else {
			System.out.println("***"+location+"kumhoTire 펑크 ***");
			return false;
		}
	}

}​
package com.day3;

public class Car {
	//필드
	Tire frintLeftTire = new Tire("앞왼쪽",6);
	Tire frintRightTire = new Tire("앞오른쪽",2);
	Tire backLeftTire = new Tire("뒤왼쪽",3);
	Tire backRightTire = new Tire("뒤왼쪽",4);
	//생성자
	//메소드
	int run() {
		System.out.println("[자동차가 달립니다.]");
		if(frintLeftTire.roll()==false) { stop(); return 1; }
		if(frintRightTire.roll()==false) { stop(); return 2; }
		if(backLeftTire.roll()==false) { stop(); return 2; }
		if(backRightTire.roll()==false) { stop(); return 2; }
		return 0;
	}

	
	void stop() {
		System.out.println("[자동차가 멈춥니다]"); 
	}
}
package com.day3;

public class Tire {
	//필드
	public int maxRotation; //최대회전수
	public int accumulatedRotstion; //누적회전수
	public String location;//타이어의 위치
	
	//생성자
	public Tire(String location, int maxRotation) {
		this.location = location;
		this.maxRotation = maxRotation;
	}
	
	//메소드 
	public boolean roll() {
		++accumulatedRotstion;  //누적 회전수 1 증가
		if(accumulatedRotstion<maxRotation) {
			System.out.println(location+"Tire 수명:"+
		(maxRotation-accumulatedRotstion)+ "회");
			return true;
		}else {
			System.out.println("***"+location+"Tire 펑크 ***");
			return false;
		}
	}
	

}
package com.day3;

public class HankookTire extends Tire {
	//필드
	//생성자
	public HankookTire(String lication, int maxRotation) {
		super(lication, maxRotation);
	}
	//메소드
	@Override
	public boolean roll() {
		++accumulatedRotstion;
		if(accumulatedRotstion<maxRotation) {
			System.out.println(location + "HankookTire 수명:"+
		(maxRotation-accumulatedRotstion)+ "회");
			return true;
		}else {
			System.out.println("***"+location+"HankookTire 펑크 ***");
			return false;
		}
	}

}
package com.day3;

public class KumhoTire extends Tire {
	//필드
	//생성자
	public KumhoTire(String location, int maxRotation) {
		super(location, maxRotation);
	}
	//메소드
	@Override
	public boolean roll() {
		++accumulatedRotstion;
		if(accumulatedRotstion<maxRotation) {
			System.out.println(location+ "KumhoTire 수명"+
		(maxRotation-accumulatedRotstion) + "회");
			return true;
		}else {
			System.out.println("***"+location+"kumhoTire 펑크 ***");
			return false;
		}
	}

}

728x90
Comments