士兵射击类

枪支类

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

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值