- 作者:xiaoxiao
- 发表时间:2020-12-23 10:53
- 来源:未知
//MyCompositeusing System;using System.Collections;
//----------------------------------Class FileElementabstract class CFileElement{ //Fields protected string name; public CFileElement(string name) { this.name=name; }
public abstract void Add(CFileElement e); public abstract void Remove(CFileElement e); public abstract void Display();};
//Class CFileclass CFile:CFileElement{ public CFile(string name):base(name){} public override void Add(CFileElement e) { Console.WriteLine("Can't add any files under a file!"); } public override void Remove(CFileElement e) { Console.WriteLine("Can't remove any sub files!"); } public override void Display() { Console.WriteLine(this.name); }};
//Class CFolderclass CFolder:CFileElement{ private ArrayList children =new ArrayList();
public CFolder(string name):base(name){}
public override void Add(CFileElement e) { children.Add(e); } public override void Remove(CFileElement e) { children.Remove(e); } public override void Display() { Console.WriteLine(this.name); foreach(CFileElement e in children) { e.Display(); } }
};//----------------------------------Class Element
//TestAppclass TestApp{ public static void Main(string[] args) { CFolder root = new CFolder("Z:/"); CFolder afolder=new CFolder("Games/"); afolder.Add(new CFile("cstrike.exe")); root.Add(afolder); root.Display(); while(true); }};