标签:str color 父类 animal pre ima OLE sum []
/// <summary> /// 抽象类 /// </summary> public abstract class Animal { public abstract void Show(); }
public class Dog : Animal { public override void Show() { Console.WriteLine("我是狗"); } }
/// <summary> /// 装饰器吃的
/// <summary> /// 装饰器爱好 /// </summary> public class HobbyDecorator : Animal { private Animal _animal = null; public HobbyDecorator(Animal animal) {
static void Main(string[] args) { ///装饰器添加属性 Animal dog = new Dog(); dog = new HobbyDecorator(dog); dog = new EatDecorator(dog); dog.Show(); Console.ReadKey(); }
this._animal = animal; } public override void Show() { //调用父类的方法 this._animal.Show(); Console.WriteLine("比较喜欢吃屎"); } }
方法 /// </summary> public class EatDecorator : Animal { public Animal _animal = null; public EatDecorator(Animal animal) { _animal = animal; } public override void Show() { _animal.Show(); Console.WriteLine("喜欢吃骨头"); } }
标签:str color 父类 animal pre ima OLE sum []
原文地址:https://www.cnblogs.com/zxp6/p/9291916.html