实验目的:
使用类来封装对象的属性和行为
Tank.java
public class Tank {
double speed;
int bulletAmount;
void speedUp(int s){
speed=s+speed;
}
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("没有炮弹了,无法开火!");
}
}
}
fight.java
public class Fight {
public static void main(String[] args) {
Tank tank1,tank2;
tank1=new Tank();
tank2=new Tank();