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(); } } }

浙公网安备 33010602011771号