這次我們來看一個小案例,有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類的對象,然後彈夾需要裝子彈,子彈的數量由自己決定。