枪支类
package shibing;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
public class Gun {
private String name;//名字
private int bullet;//子弹
public Gun() {
}
public Gun(String name, int bullet) {
this.name = name;
this.bullet = bullet;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getBullet() {
return bullet;
}
public void setBullet(int bullet) {
this.bullet = bullet;
}
public static void shoot(){
System.out.println("装子弹成功");
}
}
士兵类
package shibing;
public class People {
private String name;//人名
private String gun_name;//枪名
public People() {
}
public People(String name, String gun_name) {
this.name = name;
this.gun_name = gun_name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGun_name() {
return gun_name;
}
public void setGun_name(String gun_name) {
this.gun_name = gun_name;
}
public static void shoot(){
System.out.println("开枪射击目标");
}
}
测试类
package shibing;
public class Test {
public static void main(String[] args) {
People people = new People();
people.setName("小影");
Gun gun = new Gun("巴雷特A21",5);
people.setGun_name(gun.getName());
if (people.getName().isEmpty() || people.getGun_name().isEmpty()){
System.out.println("无法射击");
}else if(gun.getBullet() == 0) {
System.out.println("无法射击");
}else {
//调用装子弹方法
System.out.println("士兵" + people.getName() + ":");
Gun.shoot();
//调用开枪方法
System.out.println(people.getGun_name() + "瞄准目标:");
People.shoot();
}
}
}
新增知识点
isEmpty()方法判断字符串是否为空,为空返回true,否则返回false