속성 : 제조사(maker), 모델 이름(model), 색상(color), 현재 속도(curSpeed)
행위 : 가속(speedUp), 감속(speedDown)
package chapter20230822.test08;
class Car {
private String maker, model, color;
protected int curSpeed;
public Car(String maker, String model, String color, int curSpeed) { // 생성자
this.maker = maker; // 어디다가 저장할건지
this.model = model;
this.color = color;
this.curSpeed = curSpeed;
}
public void speedUp() { // 가속메서드
this.curSpeed ++;
}
public void speedDown() { // 감속메서드
this.curSpeed --;
}
@Override
public String toString() {
return "Car [maker=" + maker + ", model=" + model + ", color=" + color + ", curSpeed=" + curSpeed + "]";
}
}
class SportCar extends Car {
public SportCar (String maker, String model, String color, int curSpeed) {
super(maker, model, color, curSpeed);
}
@Override
public void speedUp() {
this.curSpeed += 10;
}
@Override
public void speedDown() {
this.curSpeed -= 10;
}
}
public class test_car {
public static void main(String[] args) {
Car car1 = new Car("현대", "소나타", "검정", 0);
Car car2 = new Car("테슬라", "모델1", "파랑", 0);
SportCar sportCar = new SportCar("아우디", "a6", "회색", 0);
System.out.println(car1);
System.out.println(car2);
}
}