首页 > 开发 > 综合 > 正文

My Composite in C#

2024-07-21 02:19:13
字体:
来源:转载
供稿:网友
//mycomposite
using system;
using system.collections;

//----------------------------------class fileelement
abstract 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 cfile
class 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 cfolder
class 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

//testapp
class 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);
}
};


最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表