zoukankan      html  css  js  c++  java
  • 装饰器模式

        /// <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("喜欢吃骨头");
            }
        }
  • 相关阅读:
    Java annotation
    子类 父类强转 HttpServlet service实现
    父类 子类 强转
    HttpServlet Service方法
    java go
    IO写 PrintWriter
    IO读 BufferedReader+FileReader
    Java NIO-3
    心跳包(HeartBeat)
    Git学习笔记(一)
  • 原文地址:https://www.cnblogs.com/zxp6/p/9291916.html
Copyright © 2011-2022 走看看