.net设计模式之装饰模式(Decorator)(2)

/// <summary> /// 技能栏,继续学技能 /// </summary> public abstract class SkillDecorator : Hero { private Hero hero; public string skillName; public SkillDecorator(Hero hero, string skillName) { this.hero = hero; this.skillName = skillName; } public override void LearnSkill() { if (hero != null) { hero.LearnSkill(); } } } /// <summary> /// 具体的技能Q /// </summary> public class QSkill : SkillDecorator { public QSkill(Hero hero, string skillName) : base(hero, skillName) { } public override void LearnSkill() { LearnQSkill(); base.LearnSkill(); } /// <summary> /// Q 技能 特性 /// </summary> public void LearnQSkill() { Console.WriteLine("学习了{0}技能!", skillName); } } /// <summary> /// 具体的技能W /// </summary> public class WSkill : SkillDecorator { public WSkill(Hero hero, string skillName) : base(hero, skillName) { } public override void LearnSkill() { LearnWSkill(); base.LearnSkill(); } /// <summary> /// W 技能 特性 /// </summary> public void LearnWSkill() { Console.WriteLine("学习了{0}技能!", skillName); } }

客户端

Hero jugg = new JUGG("剑圣"); SkillDecorator q = new QSkill(jugg, "剑刃风暴"); SkillDecorator w = new WSkill(q, "治疗守卫"); w.LearnSkill();

结果

.net设计模式之装饰模式(Decorator)

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdsxpp.html