发布网友 发布时间:2022-04-26 20:29
共1个回答
热心网友 时间:2023-10-29 18:05
1.public abstract class Bing {
private String bingType;
public Bing(String bingType)
{
this.bingType=bingType;
}
public String getType()
{
return bingType;
}
abstract void atract();
}
class QiBing extends Bing{
public QiBing(String bingType) {
super(bingType);
}
@Override
void atract() {
System.out.println(this.getType()+"骑马拿刀砍人");
}
}
class BuiBing extends Bing{
public BuiBing(String bingType) {
super(bingType);
}
@Override
void atract() {
System.out.println(this.getType()+"拿刀砍人");
}
}
class GongBing extends Bing{
public GongBing(String bingType) {
super(bingType);
}
@Override
void atract() {
System.out.println(this.getType()+"拿剑射人");
}
}
interface Signal{
public void SendSignal();
}
class JuGuan extends Bing implements Signal{
public JuGuan(String bingType) {
super(bingType);
}
@Override
public void SendSignal() {
System.out.println("军官开始发进攻信号了");
}
@Override
void atract() {
}
}
2.
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
class DaZhang{
List<Bing> list=new ArrayList<Bing>();
public void addSiBing(Bing bing)
{
list.add(bing);
}
public void display()
{
Iterator<Bing> iterator=list.iterator();
while(iterator.hasNext())
{
iterator.next().atract();
}
}
}
public class Text {
public static void main(String[] args) {
DaZhang daZhang=new DaZhang();
JuGuan juGuan=new JuGuan("军官");
Bing b1=new QiBing("骑兵");
Bing b2=new QiBing("骑兵");
Bing b3=new QiBing("骑兵");
Bing b4=new BuiBing("步兵");
Bing b5=new BuiBing("步兵");
Bing b6=new BuiBing("步兵");
Bing b7=new GongBing("弓箭手");
Bing b8=new GongBing("弓箭手");
Bing b9=new GongBing("弓箭手");
Bing b10=new GongBing("弓箭手");
daZhang.addSiBing(b1);
daZhang.addSiBing(b2);
daZhang.addSiBing(b3);
daZhang.addSiBing(b4);
daZhang.addSiBing(b5);
daZhang.addSiBing(b6);
daZhang.addSiBing(b7);
daZhang.addSiBing(b8);
daZhang.addSiBing(b9);
daZhang.addSiBing(b10);
juGuan.SendSignal();
daZhang.display();
}
}
3.显示的结果: