모든 하위 클래스에서 해당 값을 변경할 수 있으므로 부모 클래스에서 보호 된 변수를 사용하는 것은 좋지 않은 디자인이라는 것을 알고 있습니다. 그러나, 나는 그것을 시험해보고자했지만 나는 여기서 잘못된 것을하고있다. Truck 클래스에서 기호 speed = 999999;
을 찾을 수 없다는 것을 알려줍니다. 하위 클래스가 부모 클래스의 보호 된 변수 speed
에 액세스 할 수 있다고 생각했습니다.자식 클래스 Java에서 부모 클래스의 보호 된 변수 변경
public class Vehicle {
protected double speed;
protected double maxSpeed;
public Vehicle(double speed, double maxSpeedIn) throws InvalidDataException{
setSpeed(speed);
maxSpeed = maxSpeedIn;
}
public void setSpeed(double s) throws InvalidDataException {
if (s < 0.0) {
throw new InvalidDataException("Negative speed is not valid");
}
if (s > maxSpeed) {
throw new InvalidDataException("Speed cannot exceed maximum spped:");
}
speed = s;
}
}
public class Truck extends Vehicle {
public Truck(double speedin, double maxSpeedin) throws InvalidDataException {
super(speedin,maxSpeedin);
}
speed = 999999;
}
지금 고맙습니다! – Dan