⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 program.cs

📁 这是asp.net^和Visual C++Sharp编写的串并口通讯的书籍 源代码
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.Text;

using System.Collections;

namespace _1
{
    abstract class AComponent
    {
        protected string name;
        public AComponent(string name)
        { this.name = name; }
        abstract public void Add(AComponent c);
        abstract public void Remove(AComponent c);
        abstract public void Display(int AComponent);
    }
    // 合成
    class Composite : AComponent
    {
        private ArrayList children = new ArrayList();
        public Composite(string name) : base(name) { }
        public override void Add(AComponent component)
        { children.Add(component); }
        public override void Remove(AComponent component)
        { children.Remove(component); }
        public override void Display(int AComponent)
        {
            Console.WriteLine(new String('+', AComponent) + name);
            // 输出每个子节点
            foreach (AComponent component in children)
                component.Display(AComponent + 2);
        }
    }
    // 子项
    class Leaf : AComponent
    {
        public Leaf(string name) : base(name) { }
        public override void Add(AComponent c)
        { Console.WriteLine("不能添加子项!"); }

        public override void Remove(AComponent c)
        { Console.WriteLine("不能移出子项!"); }

        public override void Display(int AComponent)
        { Console.WriteLine(new String('-', AComponent) + name); }
    }
    public class Client
    {
        public static void Main(string[] args)
        {
            // 建立一个树型结构
            Composite root = new Composite("根目录");
            root.Add(new Leaf("子项 A"));
            root.Add(new Leaf("子项 B"));
            Composite comp = new Composite("合成 X");
            comp.Add(new Leaf("子项 XA"));
            comp.Add(new Leaf("子项 XB"));
            root.Add(comp);
            root.Add(new Leaf("子项 C"));
            //添加和移出一个子项
            Leaf l = new Leaf("子项 D");
            root.Add(l);
            root.Remove(l);
            // 递归输出子节点
            root.Display(1);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -