Person (abstract)
├── Teacher
└── Student
public enum SexType { Male, Female, Unknown }
public enum ProTitle { Assistant, Lecturer, AssociateProfessor, Professor, Unknown }
public enum StuTitle { Monitor, ViceMonitor, StudyCommittee, Member }
public struct CourseScore {
public string CourseName;
public float Score;
}
public abstract class Person {
// 私有欄位
private string name;
private float height;
private float weight;
private SexType sex;
private string id; // 18 碼身分證
// 屬性
public string Name { get => name; set => name = value; }
public float Height { get => height; set => height = value; }
public float Weight { get => weight; set => weight = value; }
public SexType Sex { get => sex; set => sex = value; }
public string ID {
get => id;
set {
if (value.Length == 18 && !value.Contains(" "))
id = value;
else
throw new ArgumentException("身分證號格式錯誤");
}
}
public int Age { get; private set; } // 唯讀,實作略
// 建構函式
public Person() {
name = "Unknown";
height = weight = 0;
sex = SexType.Unknown;
ID = "999999999999999999";
Age = 0;
}
public Person(string name, string id, float height, float weight, SexType sex) {
Name = name;
Height = height;
Weight = weight;
Sex = sex;
ID = id;
// Age 由生日計算,略
}
// 抽象方法
public abstract void Saying();
public abstract void Write(FileStream fs);
public abstract void Read(FileStream fs);
}
public class Teacher : Person {
private ProTitle title;
private List<string> courses = new List<string>();
public ProTitle Title { get => title; set => title = value; }
public int CourseNumber => courses.Count; // 唯讀
public Teacher() : base() { }
public Teacher(string name, string id, float height, float weight, SexType sex, ProTitle title)
: base(name, id, height, weight, sex) {
this.title = title;
}
// 新增課程(不可重複)
public string CourseAdd(string courseName) {
if (courses.Contains(courseName))
return $"課程 [{courseName}] 已存在";
courses.Add(courseName);
return "新增成功";
}
// 刪除課程
public string CourseDelete(string courseName) {
if (!courses.Remove(courseName))
return $"課程 [{courseName}] 不存在";
return "刪除成功";
}
public override void Saying() {
Console.WriteLine("我正在講課,不方便接電話");
}
public override string ToString() {
return $"教師: {Name}, 職稱: {Title}, 課程: {string.Join(",", courses)}";
}
public override void Write(FileStream fs) { /* 實作略 */ }
public override void Read(FileStream fs) { /* 實作略 */ }
}
public class Student : Person {
private string stuID;
private List<CourseScore> courseScores = new List<CourseScore>();
private StuTitle title;
public string StuID { get => stuID; set => stuID = value; }
public StuTitle Title { get => title; set => title = value; }
public int CourseNumber => courseScores.Count;
public Student() : base() { }
public Student(string name, string id, float height, float weight, SexType sex, string stuID, StuTitle title)
: base(name, id, height, weight, sex) {
this.stuID = stuID;
this.title = title;
}
// 重載新增課程
public void CourseAdd(string courseName, float score) {
courseScores.Add(new CourseScore { CourseName = courseName, Score = score });
}
public void CourseAdd(CourseScore course) {
courseScores.Add(course);
}
// 統計分數
public void GetScore(ref double high, ref double low, ref double avg) {
if (courseScores.Count == 0) { high = low = avg = 0; return; }
high = courseScores.Max(c => c.Score);
low = courseScores.Min(c => c.Score);
avg = courseScores.Average(c => c.Score);
}
public override void Saying() {
Console.WriteLine("我正在上課,不方便接電話");
}
public override string ToString() {
var sb = new StringBuilder();
sb.AppendLine($"學生: {Name}, 學號: {StuID}, 身份: {Title}");
foreach (var c in courseScores)
sb.AppendLine($" {c.CourseName}: {c.Score}");
return sb.ToString();
}
public override void Write(FileStream fs) { /* 實作略 */ }
public override void Read(FileStream fs) { /* 實作略 */ }
}
Person[] people = { new Teacher(...), new Student(...) }; foreach(var p in people) p.Saying();CourseScore 為值型別,適合高頻小物件。Admin、Parent 只需繼承 Person 並實作抽象方法。[TestMethod]
public void TestTeacherCourseManagement() {
var t = new Teacher("王老師", "110...", 1.7f, 65f, SexType.Male, ProTitle.Professor);
Assert.AreEqual("新增成功", t.CourseAdd("C#進階"));
Assert.AreEqual("課程 [C#進階] 已存在", t.CourseAdd("C#進階"));
Assert.AreEqual("刪除成功", t.CourseDelete("C#進階"));
Assert.AreEqual("課程 [Java] 不存在", t.CourseDelete("Java"));
}
此架構清晰分離關注點,符合 SOLID 原則,適合中型教務系統核心域模型。