前面是Car类和CarStatus类,后面是测试类TestCar,问题:最后面ff的两次赋值,只能赋一次,第二次编译时就会出现错误: 无法访问的语句 ff = trafficAdmin.isLegalSpeed(trafficAdmin.getCarStatus(normalCar)); 不知为什么?大神帮帮忙!!!
public class Car {
String name = "haha";
String color;
String direction;
int speed;
boolean isTrafficAdmin;
public Car() {
this(50,"beijing");
}
public Car(int p_speed,String p_direction) {
this(p_speed,p_direction,false);
}
public Car(int p_speed,String p_direction,boolean isTrafficAdmin) {
this.speed = p_speed;
this.direction = p_direction;
this.isTrafficAdmin = isTrafficAdmin;
}
public void dirveCar() {
speed = 50;
direction = "beijing";
}
public void raiseSpeed (int p_speed) {
if(p_speed < 0) {
System.out.println("加速度不能为负数");
return;
}else {
int currentspeed = speed + p_speed;
speed = currentspeed;
}
}
public void raiseSpeed (byte p_speed) {
if(p_speed < 0) {
System.out.println("加速度不能为负数");
return;
}else {
int currentspeed = speed + p_speed;
speed = currentspeed;
}
}
public void overTakeCar( Car another) {
speed = another.speed + 5;
direction = another.direction;
}
public void setSpeedAndDirection(int p_speed,String direction) {
speed = p_speed;
this.direction = direction;
}
public boolean isOverSpeed() {
if (speed > 60) {
return true;
}else
return false;
}
public void setAnotherCarName(Car another,String newName) {
if(!isTrafficAdmin) {
System.out.println("你没有权限");
return;
}else {
another.name = newName;
}
}
public void setSpeed(Car another) {
if(!isTrafficAdmin) {
System.out.println("你没有权限");
return;
}else {
another.speed = 0;
}
}
public CarStatus getCarStatus(Car another) {
CarStatus status = new CarStatus(another.speed,another.direction);
return status;
}
public boolean isLegalSpeed(CarStatus status) {
if(status.speed<=60) {
return true;
}else {
return false;
}
}
public void printCar() {
System.out.println(speed);
System.out.println(direction);
}
}
public class CarStatus {
int speed;
String direction;
public CarStatus() {
}
public CarStatus(int p_speed,String direction) {
this.speed = p_speed;
this.direction = direction;
}
}
public class TestCar {
public static void main(String[] args) {
Car normalCar = new Car();
normalCar.printCar();
Car crazyCar = new Car();
crazyCar.printCar();
Car trafficAdmin = new Car(40,"beijing",true);
trafficAdmin.printCar();
crazyCar.raiseSpeed(10);
crazyCar.printCar();
crazyCar.raiseSpeed((byte)10);
crazyCar.printCar();
normalCar.overTakeCar(crazyCar);
normalCar.printCar();
normalCar.setSpeedAndDirection(50,"beijing");
normalCar.printCar();
/*boolean ff = trafficAdmin.isLegalSpeed(trafficAdmin.getCarStatus(normalCar));
if(ff) {
trafficAdmin.setAnotherCarName(normalCar,"good");
System.out.println(normalCar.name);
return;
}else {
return;
}*/
//normalCar.printCar(); 这句怎么无法访问呢?
boolean ff = trafficAdmin.isLegalSpeed(trafficAdmin.getCarStatus(crazyCar));
if(ff) {
trafficAdmin.setAnotherCarName(normalCar,"good");