二、某公司管理組織以樹狀結構表示。C#程式碼如下所示。員工
(Employee)包括管理者(Manager)和開發人員(Developer):(1)
Manager 有自己管理的團隊,可以管理 Manager 和 Developer;(2)
Developer 被 Manager 所管理。(共 30 分)(一) 請劃出程式碼中組織樹狀圖,並說明之:(7 分)
abstract class Employee {
public abstract void add(Employee employee);
public abstract void remove(Employee employee);
public abstract void myPrint();
public virtual void setDep(int dep) { this.dep = dep; }
public void print() {
Console.WriteLine(dep + ":" + Name + ", Salary = " + Salary);
myPrint();
}
public String Name { get; set; }
public int Salary { get; set; }
protected int dep;
private String name;
private int salary;
}
class Manager: Employee {
public Manager(String name, int salary) {
Name = name;
Salary = salary;
setDep(1);
}
public override void setDep(int dep) {
this.dep = dep;
foreach (Employee employee in employees) {
employee.setDep(this.dep + 1);
}
}
public override void add(Employee employee) {
employee.setDep(dep + 1);
employees.Add(employee);
}
public override void myPrint() {
if (employees.Count>0) {
Console.WriteLine("The management team " + dep);
foreach (Employee employee in employees) {
employee.print();
}Console.WriteLine("The team end " + dep);
}
}
public override void remove(Employee employee) {
employees.Remove(employee);
}
private List<Employee> employees = new List<Employee>();
}
class Developer: Employee {
public Developer(String name, int salary) {
Name = name;
Salary = salary;
setDep(1);
}
public override void myPrint() { /*leaf node is not applicable*/ }
public override void add(Employee employee) { /*leaf node is not applicable*/ }
public override void remove(Employee employee) { /*leaf node is not applicable*/ }
}
class Program {
static void Main(string[] args) {
Employee Tom = new Manager("Tom", 90000);
Employee Mary = new Manager("Mary", 80000);
Employee John = new Manager("John", 70000);
Employee Kevin = new Developer("Kevin", 50000);
Employee Sam = new Developer("Sam", 40000);
Employee Bob = new Developer("Bob", 40000);
Mary.add(Kevin);
Mary.add(Sam);
John.add(Bob);
Tom.add(Mary);
Tom.add(John);
John.print();
Tom.print();
Console.Read();
}
}
【題組】
(二) 請寫出上述程式碼的輸出。(8 分)