//方法三:以父類別為類別,以子類別為其值的型別 public static void ThreeDispaly() { Staff Peter = new FullWork(); Staff fullWorkder = new Provisional(); Peter.CalcMoney(); //呼叫父類別的方法 fullWorkder.CalcMoney(); //呼叫子類別的方法 }
/* 方法二:輸出正式員工月薪、兼職員工時薪 * 實體化子類別物件並直接存取本身所定義的方法 * 修飾詞用了new和override)calcMoney, * 分別算出正式員工的月薪和兼職員工的時薪 */ public static void SecDisplay() { FullWork fullWorker = new FullWork(); fullWorker.CalcMoney(); Provisional partWork = new Provisional(); partWork.CalcMoney(); }
//方法一:實作各物件並呼叫showMessage(),只有兼職員工算出時薪 public static void NonDisplay() { Staff Peter = new Staff(); Peter.ShowMessage(); FullWork fullWorker = new FullWork(); fullWorker.ShowMessage(); Provisional partWork = new Provisional(); partWork.ShowMessage(); //使用覆寫,算出時薪 }