集合经典一练——模拟一个军队作战,军队里面有各种兵,骑兵,步兵。每种兵的攻击敌人方式不一样。建立各种各样的兵。

模拟一个军队作战,军队里面有各种兵,骑兵,步兵。每种兵的攻击敌人方式不一样。
建立各种各样的兵。每种3个,至少3种,然后建立一个军官。
军官也会上场去攻击敌人,但是军官还有一种行为,就是发号,军官发号了,所有的兵,无论什么兵都去战场攻击敌人。
最后程序运行效果就是一个军官开始发号,然后所有的士兵包括军官都去打仗,显示每个人攻击敌人的方式

package text;
import java.util.*;

public class Text_6 {
		public static void main(String[] args) {
			PeoPle PeoPle=new PeoPle();
			JuGuan juGuan=new JuGuan("军官");
			AllBing b1=new QiBing("骑兵1");
			AllBing b2=new QiBing("骑兵2");
			AllBing b3=new QiBing("骑兵3");
			AllBing b4=new BuBing("步兵1");
			AllBing b5=new BuBing("步兵2");
			AllBing b6=new BuBing("步兵3");
			AllBing b7=new FlyBing("飞行兵1");
			AllBing b8=new FlyBing("飞行兵2");
			AllBing b9=new FlyBing("飞行兵3");
			PeoPle.addIt(juGuan);
			PeoPle.addIt(b1);
			PeoPle.addIt(b2);
			PeoPle.addIt(b3);
			PeoPle.addIt(b4);
			PeoPle.addIt(b5);
			PeoPle.addIt(b6);
			PeoPle.addIt(b7);
			PeoPle.addIt(b8);
			PeoPle.addIt(b9);
			juGuan.Say();
			PeoPle.display();
		}
}
abstract class AllBing {
		private String Type;
		public AllBing(String Type){
			this.Type=Type;
		}
		public String getType() {
			return Type;
		}
		public void setType(String type) {
			Type = type;
		}

	public abstract void atract();
}

class QiBing extends AllBing{
		public QiBing(String Type) {
			super(Type);
		}
		@Override
		public void atract() {
			System.out.println(this.getType()+"正在骑马拿刀冲锋攻击敌人");//(骑马率领人可佩戴手枪)
		}
}

class BuBing extends AllBing{
		public BuBing(String Type) {
			super(Type);
		}
		@Override
		public void atract() {
			System.out.println(this.getType()+"正在使用三八大盖和手雷阻击敌人");//(三八大盖又俗称 98K )
		}
}

class FlyBing extends AllBing{
		public FlyBing(String Type) {
			super(Type);
		}
		@Override
		public void atract() {
			System.out.println(this.getType()+"正在驾驶飞机空投核弹炸敌人");
		}
}

interface Signal{
		public void Say();
}

class JuGuan extends AllBing implements Signal{
		public JuGuan(String Type) {
			super(Type);
		}
		@Override
		public void Say() {
			System.out.println("军官发号:所有人准备,拿出你们的勇气,消灭敌人,给我杀,冲啊!");
		}
		@Override
		public void atract() {
			System.out.println(this.getType()+"军官正在双持手枪在中间杀敌");
		}
}

class PeoPle{
		ArrayList<AllBing> list=new ArrayList<AllBing>();
		public void addIt(AllBing ab){
			list.add(ab);
		}
		public void display(){
			Iterator<AllBing> it=list.iterator();
			while(it.hasNext()){
				it.next().atract();
			}
		}
}


在这里插入图片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章