Company.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
abstract class Company
{
protected string name;
public Company(string name)
{
this.name = name;
}
public abstract void Add(Company c);
public abstract void Remove(Company c);
public abstract void Display(int depth);
public abstract void LineOfDuty();
}
}
ConcreateCompany.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class ConcreateCompany:Company
{
private List<Company> children = new List<Company>();
public ConcreateCompany(string name):base(name)
{
}
public override void Add(Company c)
{
children.Add(c);
}
public override void Remove(Company c)
{
children.Remove(c);
}
public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);
foreach(Company component in children)
{
component.Display(depth + 2);
}
}
public override void LineOfDuty()
{
foreach(Company component in children)
{
component.LineOfDuty();
}
}
}
}
HRDepartment.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class HRDepartment:Company
{
public HRDepartment(string name) : base(name)
{
}
public override void Add(Company c)
{
}
public override void Remove(Company c)
{
}
public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);
}
public override void LineOfDuty()
{
Console.WriteLine("{0}1.员工招聘 2.培训管理", name);
}
}
}
FinanceDepartment.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class FinanceDepartment:Company
{
public FinanceDepartment(string name) : base(name)
{
}
public override void Add(Company c)
{
}
public override void Remove(Company c)
{
}
public override void Display(int depth)
{
Console.WriteLine(new String('-', depth) + name);
}
public override void LineOfDuty()
{
Console.WriteLine("{0}1.公司财务 2.收支管理", name);
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo
{
class Program
{
static void Main(string[] args)
{
ConcreateCompany root = new ConcreateCompany("柳州总公司");
root.Add(new FinanceDepartment("总公司财务部"));
root.Add(new HRDepartment("总公司人力资源部"));
ConcreateCompany comp = new ConcreateCompany("杭州华东分公司");
comp.Add(new FinanceDepartment("华东分公司财务部"));
comp.Add(new HRDepartment("华东分公司人力资源部"));
ConcreateCompany comp1 = new ConcreateCompany("上海办事处");
comp1.Add(new FinanceDepartment("上海办事处财务部"));
comp1.Add(new HRDepartment("上海办事处人力资源部"));
ConcreateCompany comp2 = new ConcreateCompany("北京办事处");
comp2.Add(new FinanceDepartment("北京办事处财务部"));
comp2.Add(new HRDepartment("北京办事处人力资源部"));
root.Add(comp);
comp.Add(comp1);
comp.Add(comp2);
Console.WriteLine("组织架构:\n");
root.Display(1);
Console.WriteLine("职责:\n");
root.LineOfDuty();
Console.Read();
}
}
}
运行结果: