/**
* 攻击类
*/
public interface Attack {
public void attack(Hero hero);
}
public class JsAttack implements Attack {
public void attack(Hero hero) {
System.out.println(hero.getName()+" 使用技能—— "+hero.getSkill()+" 使用武器是剑");
}
}
public class MzAttack implements Attack {
public void attack(Hero hero) {
System.out.println(hero.getName()+" 使用技能—— "+hero.getSkill()+" 使用武器是刀");
}
}
/**
* 英雄类
*/
public class Hero {
private String name;
private String skill;
public Hero(){}
public Hero(String name, String skill) {
this.name = name;
this.skill = skill;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
}
/**
* 选择英雄
*/
public class SelectHero {
public static void main(String[] args) {
Hero js = new Hero("剑圣","无极之道");
Hero mz = new Hero("蛮三刀","无尽怒火");
JsAttack jsAttack = new JsAttack();
jsAttack.attack(js);
MzAttack mzAttack = new MzAttack();
mzAttack.attack(mz);
}
}
运行结果:
剑圣 使用技能—— 无极之道 使用武器是剑
蛮三刀 使用技能—— 无尽怒火 使用武器是刀