不好意思,没仔细看,你的speedUp不是属性,是个方法,7楼的写法可以
public class JavaApplication5 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
Tank tank1 = new Tank();
Tank tank2 = new Tank();
tank1.setBulletAmount(10);
tank2.setBulletAmount(1);
tank1.speedUp(input.nextDouble());
tank2.speedUp(input.nextDouble());
tank1.fire();
tank2.fire();
System.out.println("tank1目前的速度:" + tank1.getSpeed());
System.out.println("tank2目前的速度:" + tank2.getSpeed());
System.out.println("tank1现有炮弹数量:" + tank1.getBulletAmount());
System.out.println("tank2现有炮弹数量:" + tank2.getBulletAmount());
tank1.speedUp(input.nextDouble());
tank2.speedUp(input.nextDouble());
tank1.fire();
tank2.fire();
System.out.println("tank1目前的速度:" + tank1.getSpeed());
System.out.println("tank2目前的速度:" + tank2.getSpeed());
System.out.println("tank1的炮弹数量:" + tank1.getBulletAmount());
System.out.println("tank2的炮弹数量:" + tank2.getBulletAmount());
}
}
class Tank {
private double speed;
private int bulletAmount;
void speedUp(double s) {
speed = speed + s;
}
void speedDown(int d) {
if (speed - d >= 0)
speed = speed - d;
else
speed = 0;
}
void setBulletAmount(int m) {
bulletAmount = m;
}
int getBulletAmount() {
return bulletAmount;
}
double getSpeed() {
return speed;
}
void fire() {
if (bulletAmount >= 1) {
bulletAmount = bulletAmount - 1;
System.out.println("打出一发炮弹");
} else {
System.out.println("没有炮弹了");
}
}
}