package test_class;
public class Hero_2 {
private String name;
**private Skill skill;**
public void attack(){
System.out.println("我叫"+name+".开始释放技能");
skill.use();
System.out.println("释放技能完成");
}
public Hero_2() {
}
public Hero_2(String name, Skill skill) {
this.name = name;
this.skill = skill;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Skill getSkill() {
return skill;
}
public void setSkill(Skill skill) {
this.skill = skill;
}
}
package test_class;
public interface Skill {
void use();//释放技能
}
package test_class;
public class Skill_impl implements Skill {
@Override
public void use() {
System.out.println("biu biu biu");
}
}
package test_class;
public class demoGame {
public static void main(String[] args) {
Hero_2 hero=new Hero_2();
hero.setName("AAASH");
//设置英雄的技能
//方法1;hero.setSkill(new Skill_impl());//使用单独定义的实现类 匿名对象
// 方法 2: Skill skill=new Skill() {
// @Override
// public void use() {
// System.out.println("匿名内部类biu");
// }
// };
// hero.setSkill(skill);
hero.setSkill(new Skill() {//方法3
@Override
public void use() {
System.out.println("方法3 piapaia");
}
});
hero.attack();
}
}