a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 編程 > C# > 正文

c# 組合模式

2020-01-24 03:40:31
字體:
來源:轉載
供稿:網友
結構圖:

抽象對象:
復制代碼 代碼如下:

    abstract class Component
    {
        protected string name;
        public Component(string name)
        {
            this.name = name;
        }
        public abstract void Add(Component c);
        public abstract void Remove(Component c);
        public abstract void Display(int depth);
    }

無子節點的:
復制代碼 代碼如下:

    class Leaf : Component
    {
        public Leaf(string name)
            : base(name)
        { }
        public override void Add(Component c)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Cannot add to a Leaf");
        }
        public override void Remove(Component c)
        {
            //throw new NotImplementedException();
            Console.WriteLine("Cannot remove to a Leaf");
        }
        public override void Display(int depth)
        {
            //throw new NotImplementedException();
            Console.WriteLine(new string('-', depth) + name);
        }
    }

可以有子結點:
復制代碼 代碼如下:

    class Composite : Component
    {
        private List<Component> children = new List<Component>();
        public Composite(string name)
            : base(name)
        { }
        public override void Add(Component c)
        {
            //throw new NotImplementedException();
            children.Add(c);
        }
        public override void Remove(Component c)
        {
            //throw new NotImplementedException();
            children.Remove(c);
        }
        public override void Display(int depth)
        {
            //throw new NotImplementedException();
            Console.WriteLine(new string('-', depth) + name);
            foreach (Component component in children)
            {
                component.Display(depth + 2);
            }
        }
    }

 主函數調用:
復制代碼 代碼如下:

    class Program
    {
        static void Main(string[] args)
        {
            Composite root = new Composite("root");
            root.Add(new Leaf("Leaf A"));
            root.Add(new Leaf("Leaf B"));
            Composite comp = new Composite("Composite X");
            comp.Add(new Leaf("Leaf XA"));
            comp.Add(new Leaf("Leaf XB"));
            root.Add(comp);
            Composite comp2 = new Composite("Composite X");
            comp2.Add(new Leaf("Leaf XYA"));
            comp2.Add(new Leaf("Leaf XYB"));
            comp.Add(comp2);
            root.Display(1);
            Console.ReadKey();
        }
    }
 
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 亚洲区在线 | 韩国精品一区二区 | 午夜视频免费 | 中文字幕在线看 | 三级黄色网址 | 午夜影院入口 | 精品视频在线免费观看 | 欧美亚洲一区二区三区 | 国产一二在线 | 亚洲 自拍 另类 欧美 丝袜 | 国产91精品在线 | 99热在线观看 | 欧美在线免费 | 亚洲精品免费观看 | 亚洲一区二区三区精品视频 | 男女羞羞视频网站 | 天天干天天操天天爽 | 精品国产91亚洲一区二区三区www | 久久艹精品视频 | 日本黄色大片免费 | 天天色影院 | 国产成人综合网 | 亚洲国产在 | 亚洲1级片| 久久久一区二区 | 国产乱码精品一区二区三区中文 | 欧美精品久久久久 | www.国产.com | 成人片免费看 | 亚洲一区二区三区福利 | 欧美福利二区 | 国产影音先锋 | 欧美日韩国产不卡 | 欧美日韩综合视频 | www.久久久 | 欧美激情在线播放 | 欧美综合国产 | 日韩精品视频在线 | 日本一区二区不卡视频 | 久久久久久久久99精品 | 久久视频一区二区 |