Java 微世界大战
作者:互联网
package Fight;
public class Test {
public static void main(String[] args) {//main方法不要忘记写
Army army = new Army(4);
Tank tank= new Tank();
WuZhiJi wuZhiJi = new WuZhiJi();
GaoShePao gaoShePao =new GaoShePao();
ZhanDouJi zhanDouJi = new ZhanDouJi();
ZhanDouJi zhanDouJi1 = new ZhanDouJi();
ZhanDouJi zhanDouJi2 = new ZhanDouJi();
try {
army.AddWuQi(tank);
army.AddWuQi(wuZhiJi);
army.AddWuQi(gaoShePao);
army.AddWuQi(zhanDouJi);
army.AddWuQi(zhanDouJi1);
army.AddWuQi(zhanDouJi2);
} catch (AddWuQiException e) {
System.out.println(e.getMessage());
}
army.move();
army.attack();
}
}
package Fight;
public class Wuqi {
}
package Fight;
public interface Move {
void move();
}
package Fight;
public interface Attack {
void attack();
}
package Fight;
public class Tank extends Wuqi implements Move,Attack {
@Override
public void attack() {
System.out.println("Tank就位,等待命令开炮");
}
@Override
public void move() {
System.out.println("Tank就位,准备向目的地出发");
}
@Override
public String toString() {
return "Tank";
}
}
package Fight;
public class GaoShePao extends Wuqi implements Attack{
@Override
public void attack() {
System.out.println("高射炮就位,等待命令准备开炮");
}
@Override
public String toString() {
return "GaoShePao";
}
}
package Fight;
public class ZhanDouJi extends Wuqi implements Attack,Move{
@Override
public void attack() {
System.out.println("战斗机就位,等待命令准备开炮");
}
@Override
public void move() {
System.out.println("战斗机就位,正在向目的地进军");
}
@Override
public String toString() {
return "ZhanDouJi";
}
}
package Fight;
public class WuZhiJi extends Wuqi implements Move {
@Override
public void move() {
System.out.println("物资机就位,正在向目的地出发");
}
@Override
public String toString() {
return "WuZhiJi";
}
}
package Fight;
public class Army {
Wuqi[] wuqis;
public Army(int a) {
wuqis=new Wuqi[a];
}
public void AddWuQi(Wuqi wuqi) throws AddWuQiException {
for(int j=0;j< wuqis.length;j++){
if(null==wuqis[j]){
wuqis[j]=wuqi;
System.out.println(wuqi+"添加成功");
break;
}
if(j== wuqis.length-1){
throw new AddWuQiException("武器已达到上限,无法添加更多的武器");
}
}
}
public void move(){
for(int i=0;i<wuqis.length;i++){
if(wuqis[i] instanceof Move){
Move move =(Move) wuqis[i];
move.move();
}
}
}
public void attack(){
for(int i=0;i<wuqis.length;i++){
if(wuqis[i] instanceof Attack){
Attack attack = (Attack) wuqis[i];
attack.attack();
}
}
}
}
package Fight;
public class AddWuQiException extends Exception {
public AddWuQiException() {
}
public AddWuQiException(String message) {
super(message);
}
}
标签:Java,package,wuqis,void,Fight,Override,世界大战,public 来源: https://blog.csdn.net/qq_45858803/article/details/121527684