这次我们来看一个小案例,有Player类、Gun类、Bullet类、Clip类,还有一个测试类。需求是人用枪攻击另一个人,我们需要用到面向对象的思想来进行。
首先看一下这个UML类图:
然后我们来看具体的代码实现:
子弹类
package com.tedu.yadx.day17;
/**
* 子弹类
* @author qwf91
*
*/
public class Bullet {
private int hurt = 10;//子弹的伤害值
public Bullet(){
}
public Bullet(int hurt){
this.hurt = hurt;
}
//子弹击中敌人
public void hitEnemy(Player enemy){
enemy.damage(hurt);
}
}
弹夹类
package com.tedu.yadx.day17;
/**
* 弹夹类
* @author qwf91
*
*/
public class Clip {
private Bullet[] magazine;//弹仓
private int capacity = 30;//弹夹容量
private int surplus;//弹夹余量
public Clip(){
this(30);
}
public Clip(int capacity){
this.magazine = new Bullet[capacity];
this.surplus = 0;
showClip();
}
//装子弹
public void pushBullet(Bullet bullet){
if(surplus == capacity){
System.out.println(">>>弹夹已满,请勿重复装弹!");
return;
}
magazine[surplus] = bullet;
surplus++;
showClip();
}
//卸子弹
public Bullet popBullet(){
if(surplus == 0){
System.out.println(">>>弹夹已空,无法弹出子弹!");
return null;
}
Bullet bullet = magazine[surplus - 1];
magazine[surplus - 1] = null;
surplus--;
showClip();
return bullet;
}
//显示弹夹信息
public void showClip(){
System.out.printf(">>>弹夹状态:%d/%d\n",surplus,capacity);
}
}
枪类
package com.tedu.yadx.day17;
/**
* 枪类
* @author qwf91
*
*/
public class Gun {
private Clip clip;//弹夹
public Gun(){
this(null);
}
public Gun(Clip clip){
this.clip = clip;
showGun();
}
//装弹夹
public void loadClip(Clip clip){
this.clip = clip;
showGun();
}
//开枪
public void shootEnemy(Player enemy){
if(clip == null){
System.out.println(">>>枪械没有弹夹,放了一个空枪!");
return;
}
Bullet bullet = clip.popBullet();
if(bullet == null){
System.out.println(">>>枪械的弹夹已空,放了一个空枪!");
return;
}
bullet.hitEnemy(enemy);
}
//显示枪械信息
public void showGun(){
if(clip!=null){
System.out.println(">>>枪械信息:有弹夹");
}else{
System.out.println(">>>枪械信息:无弹夹");
}
}
}
Player类
package com.tedu.yadx.day17;
/**
* 玩家类
* @author qwf91
*
*/
public class Player {
private String name;
private int blood = 100;
private Gun gun;
public Player(){}
public Player(String name){
this(name,100);
}
public Player(String name,int blood){
this.name = name;
this.blood = blood;
showPlayer();
}
public void holdGun(Gun gun){
this.gun = gun;
showPlayer();
}
public void shootEnemy(Player enemy){
System.out.printf("%s向%s开了一枪\n",this.name,enemy.name);
if(gun == null){
System.out.println(">>>"+name+"没有枪,无法进行射击!");
return;
}
gun.shootEnemy(enemy);
}
public void loadClip(Clip clip){
if(gun == null){
System.out.println(">>>"+name+"没有枪,无法装弹夹!");
return;
}
gun.loadClip(clip);
}
public void damage(int hurt){
if(blood == 0){
System.out.println(">>>"+name+"已死亡,请勿鞭尸!");
return;
}
blood-=hurt;
if(blood<=0){
blood = 0;
System.out.println(">>>"+name+"已经死亡!");
}
showPlayer();
}
public void showPlayer(){
boolean isHoldGun = gun!=null;
System.out.printf(">>>玩家信息: 姓名=%s,血量=%d,持枪=%b\n",name,blood,isHoldGun);
}
}
测试类
package com.tedu.yadx.day17;
/**
* 测试类
* @author qwf91
*
*/
public class Test {
public static void main(String[] args){
Player p1 = new Player("老张",100);
Player p2 = new Player("老王",100);
Gun gun = new Gun();
p1.holdGun(gun);
Clip clip = new Clip();
for(int i = 1;i <= 30;i++){
clip.pushBullet(new Bullet(12));
}
p1.loadClip(clip);
for(int i = 1;i <= 15;i++){
p1.shootEnemy(p2);
}
}
}
关于每一个类,代码里面都写的很清楚,唯一要说的就是最后一个测试类,我们创建出来Clip类的对象,然后弹夹需要装子弹,子弹的数量由自己决定。