기록장
필드의 다형성 코드 짜본거 본문
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
'개발 > JAVA' 카테고리의 다른 글
매개 변수의 다형성 코드 짰음 (0) | 2022.05.21 |
---|---|
다향성(polymorphism) 알려줄게 (0) | 2022.05.21 |
다향성(polymorphism) 알려줄게 (0) | 2022.05.21 |
final 클래스와 final 메소드 설명해줄게 (0) | 2022.05.19 |
메소드의 재정의 (오버라이딩) (0) | 2022.05.17 |
Comments