zoukankan      html  css  js  c++  java
  • C#学习笔记-装饰模式

    题目:给顾客打扮,但是需要满足正常的穿衣风格,例如先穿了衬衣再打领带,最后在穿鞋子,这种基本要求。

    分析:

    现在将具体的衣服裤子和鞋子都分别写在不同的类里面,这样方便以后添加新的衣服,这些全部都属于服装类,所以将服装类设为父类,服装依附于人,故此服装的父类又是人。

    人的类:

     1  class Person
     2     {
     3         public Person()
     4         { }
     5         string name;
     6         public Person(string Name)
     7         {
     8             name = Name;
     9         }
    10 
    11         public virtual void Show()
    12         {
    13             Console.WriteLine(" 装扮的{0}", name);
    14         }
    15     }
    View Code

    继承于人的服装类:

     1 class Finery : Person
     2     {
     3         protected Person someone;
     4 
     5         public void Decorate(Person one)
     6         {
     7             someone = one;
     8         }
     9 
    10         public override void Show()
    11         {
    12             if (someone != null)
    13             {
    14                 someone.Show();
    15             }
    16         }
    17     }
    View Code

    继承于服装的具体各种服饰类:

     1 class TShirts : Finery
     2     {
     3         public override void Show()
     4         {
     5             Console.Write("大T恤 ");
     6             /**
     7              * 调用基类上已被其他方法重写的方法。
     8              * 指定创建派生类实例时应调用的基类构造函数。
     9              * 在静态成员中使用base和this都是不允许的
    10              */
    11             base.Show();
    12         }
    13     }
    14 
    15     class Sneakers : Finery
    16     {
    17         public override void Show()
    18         {
    19             Console.Write("破球鞋 ");
    20             base.Show();
    21         }
    22     }
    23     class BigTrouser : Finery
    24     {
    25         public override void Show()
    26         {
    27             Console.Write("垮裤 ");
    28             base.Show();
    29         }
    30     }
    31     class Suit : Finery
    32     {
    33         public override void Show()
    34         {
    35             Console.Write("西装 ");
    36             base.Show();
    37         }
    38     }
    39 
    40     class Tie : Finery
    41     {
    42         public override void Show()
    43         {
    44             Console.Write("领带 ");
    45             base.Show();
    46         }
    47     }
    48 
    49     class LeatherShoes : Finery
    50     {
    51         public override void Show()
    52         {
    53             Console.Write("皮鞋 ");
    54             base.Show();
    55         }
    56     }
    View Code

    主函数:

     1 static void Main(string[] args)
     2         {
     3             Person person = new Person("可达鸭");
     4             Console.WriteLine("
    第一种装扮: ");
     5 
     6             Sneakers a = new Sneakers();
     7             BigTrouser b = new BigTrouser();
     8             TShirts c = new TShirts();
     9 
    10             a.Decorate(person);
    11             b.Decorate(a);
    12             c.Decorate(b);
    13             c.Show();
    14 
    15             Console.WriteLine("
    第二种装扮: ");
    16 
    17             LeatherShoes d = new LeatherShoes();
    18             Tie e = new Tie();
    19             Suit f = new Suit();
    20 
    21             d.Decorate(person);
    22             e.Decorate(d);
    23             f.Decorate(e);
    24             f.Show();
    25 
    26             Console.WriteLine("
    第三种装扮:");
    27             Sneakers g = new Sneakers();
    28             LeatherShoes h = new LeatherShoes();
    29             BigTrouser i = new BigTrouser();
    30             Tie j = new Tie();
    31 
    32             g.Decorate(person);
    33             h.Decorate(g);
    34             i.Decorate(h);
    35             j.Decorate(i);
    36             j.Show();
    37 
    38             Console.Read();
    39         }
    View Code
  • 相关阅读:
    防止特殊html字符的问题(xxs攻击)方法
    asp.net 服务器Button控件使用(onclick和onclientclick使用)
    Asp:Button控件onclick事件无刷新页面提示消息
    动态添加Marquee标签,并动态赋值与属性
    asp.net 前台通过Eval()绑定动态显示样式
    asp.net 中json字符串转换
    近况
    C# fixed语句固定变量详解
    fixed说明
    Net架构必备工具列表
  • 原文地址:https://www.cnblogs.com/Aries-rong/p/7484897.html
Copyright © 2011-2022 走看看