package Yasuo; public class Gay { private String name; private Skill skill; public Gay() { } public Gay(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; } public void attack() { System.out.println(getName() + "使用技能: "); skill.use(); System.out.println("技能使用完毕"); } }
package Yasuo; public interface Skill { void use(); }
package Yasuo; public class SkillImpl implements Skill { @Override public void use() { System.out.println("斩钢闪"); } }
package Yasuo; public class demo2main { public static void main(String[] args) { Gay gay = new Gay(); gay.setName("亚索"); Skill skill = new SkillImpl(); //或者直接 gay.setSkill(new SkillImpl); gay.setSkill(skill); gay.attack(); System.out.println("============="); Skill skill2 = new Skill() { @Override public void use() { System.out.println("狂风绝息斩"); } }; gay.setSkill(skill2); gay.attack(); System.out.println("============"); gay.setSkill(new Skill() { @Override public void use() { System.out.println("踏前斩"); } }); gay.attack(); } }
原文地址:https://www.cnblogs.com/aoligei/p/12411371.html
时间: 2024-10-10 10:03:21