My Composite in C #

Source: Internet
Author: User
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);
}
};




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.