package sixweek;
public class EquipmentTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Person person=new Person();
EquipmentFactory ep=new GunFactory();
person.useEquipment(ep.getEquipment());
ep=new DaggerFactory();
person.useEquipment(ep.getEquipment());
ep=new FighterFactory();
person.useEquipment(ep.getEquipment());
}
}
/ouxiangchanping
abstract class Equipment{
public abstract void attack();
}
class Gun extends Equipment{
@Override
public void attack() {
// TODO Auto-generated method stub
System.out.println("开枪射击");
}}
class Dagger extends Equipment{
@Override
public void attack() {
// TODO Auto-generated method stub
System.out.println("刺杀");
}
}
//zengjia
class Fighter extends Equipment{
@Override
public void attack() {
// TODO Auto-generated method stub
System.out.println("战斗机发射核弹");
}
}
//wuqigongchangjiekou
interface EquipmentFactory{
Equipment getEquipment();
}
//jutigouzaozhe
class GunFactory implements EquipmentFactory{
@Override
public Equipment getEquipment() {
// TODO Auto-generated method stub
return new Gun();//返回具体的武器
}
}
//jutigouzaozhe具体的接口
class DaggerFactory implements EquipmentFactory{
@Override
public Equipment getEquipment() {
// TODO Auto-generated method stub
return new Dagger();
}
}
//zenghjia gongchang
class FighterFactory implements EquipmentFactory{
@Override
public Equipment getEquipment() {
// TODO Auto-generated method stub
return new Fighter();
}
}
class Person{
Equipment equipment ;
public void useEquipment(Equipment equipment ){
this.equipment= equipment;
this.equipment.attack();
}
}