using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WCF_Binding
{
//科目,抽象类
public abstract class Subject
{
protected string name;
public Subject(string name)
{
this.name = name;
}
public abstract void Display(int depth); //显示
}
public class FatherSubject : Subject
{
List<Subject> subjectList = new List<Subject>();
public FatherSubject(string name)
: base(name)
{
}
public void Add(Subject subject)
{
subjectList.Add(subject);
}
public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);
foreach (Subject item in subjectList)
{
item.Display(depth + 2);
}
}
}
public class LeafSubject : Subject
{
public LeafSubject(string name)
: base(name)
{
}
public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);
}
}
class CompositeTest
{
public static void MainTest(string[] args)
{
FatherSubject root = new FatherSubject("学科");
FatherSubject artroot = new FatherSubject("文科");
FatherSubject scienceroot = new FatherSubject("理科");
artroot.Add(new LeafSubject("语文"));
artroot.Add(new LeafSubject("英语"));
FatherSubject sciencechild = new FatherSubject("数学");
sciencechild.Add(new LeafSubject("基础数学"));
sciencechild.Add(new LeafSubject("线性代数"));
scienceroot.Add(sciencechild);
scienceroot.Add(new LeafSubject("物理"));
root.Add(artroot);
root.Add(scienceroot);
root.Display(0);
Console.ReadKey();
}
}
}
组合设计模式
最新推荐文章于 2025-01-28 15:05:02 发布
