Edward_jie

for you, my Hall of Frame

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace ConsoleApplication3
{
    abstract class AbstractCompnent
    {
        abstract public void operation();
    }
    class Leaf:AbstractCompnent
    {
        string str;
        public Leaf(string str)
        {
            this.str = str;
        }
        public override void operation()
        {
            Console.WriteLine(str);
        }
    }
    class Compnent:AbstractCompnent
    {
        public ArrayList list = new ArrayList();
        string str;
        public void add(AbstractCompnent c)
        {
            list.Add(c);
        }
        public Compnent(string str)
        {
            this.str = str;
        }
        public override void operation()
        {
            Console.WriteLine(str);
            foreach (AbstractCompnent a in list)
            {
                a.operation();
            }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Compnent aCompnent = new Compnent("我是主干");
            Leaf a = new Leaf("我是第一个叶子");
            Leaf b = new Leaf("我是第二个叶子");
            Leaf c = new Leaf("我是第三个叶子");
            aCompnent.add(a);
            aCompnent.add(b);
            aCompnent.add(c);
            Compnent bCompnent = new Compnent("我是第一个树枝");
            Leaf d = new Leaf("我是第一个树枝的第一个叶子");
            Leaf e = new Leaf("我是第一个树枝的第二个叶子");
            bCompnent.add(d);
            bCompnent.add(e);
            aCompnent.add(bCompnent);
            aCompnent.operation();
        }
    }
}

 

posted on 2012-07-31 17:03  Edward_诺  阅读(189)  评论(0)    收藏  举报