티스토리 뷰
반응형
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
반응형
'JAVA' 카테고리의 다른 글
190711 GUI, EventHandler 예제 (0) | 2019.07.11 |
---|---|
190711 Interface (0) | 2019.07.11 |
190709 상속, 다향성, 업캐스팅, 다운캐스팅 (0) | 2019.07.09 |
190708 class - 생성자 - setter,getter 연습 (0) | 2019.07.08 |
190703 Class이용한 성적관리 프로그램 (배열, 콘솔입력) (0) | 2019.07.03 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- IntelliJ #gradle #tomcat #spring #springmvc
- JSON
- JSON파싱
- spring error #
- 정규식
- no getter for property named
- selectbox
- jQuery
- 계좌번호정규식
- poi 엑셀
- 정규식 숫자만
- 정규식 한글만
- Failed to load resource: the server responded with a status of 404 (Not Found)
- ''찾기
- spring 엑셀
- mybatis
- 인텔리제이
- Regex
- SpringXmlModelInspection
- Spring
- 이메일 정규식
- JSON날짜
- PageNotFound - No mapping for GET
- 공백찾기
- select제어
- js
- 엑셀다운로드
- 정규식 특수문자
- poi
- POI EXCEL
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함