事例講述Visual Studio Employee類
學習Visual Studio時,你可能會遇到Visual Studio Employee類問題,Visual Studio Employee類的派生類型必須具有計算薪水操作,這里將介紹Visual Studio Employee類問題的解決方法,在這里拿出來和大家分享一下。
#t#為Visual Studio Employee類添加一個CalculatePay抽象方法,該方法返回一個十進制數。在Visual Studio Employee類的標題打開其快捷菜單,選擇“添加”子菜單和“方法”菜單命令,為新方法命名為“CalculatePay”。在“類詳細信息”窗口設置十進制數作為返回類型。該方法應該設置為抽象的。打開CalculatePay方法的快捷菜單并修改“繼承修飾符”為abstract(抽象)。然后系統顯示一個消息框要求你對這一修改進行確認,選“確定”。
Hourly Employee類應該繼承Visual Studio Employee類。這一關系用繼承線來創建,具體做法是:將繼承線從Hourly Employee類拖到Visual Studio Employee類。
***,為Hourly Employee類添加一個Pay方法。Pay方法只有一個參數,就是工作的小時數。在“類詳細信息”窗口,擴展Pay方法的行來打開 “添加參數”項,選擇“添加參數”行并輸入“Hours”作為參數名,修改類型為十進制數。添加一個HourlyRate屬性到該類以使其完整,該屬性也是十進制數類型的。
以下代碼由類關系圖創建。創建的函數是很粗略的,接下來只需要實現這些粗略的函數。
- public interface Iemployee
- {
- int Age
- {
- get;
- set;
- }
- Name Fullname
- {
- get;
- set;
- }
- string EmployeeInfo();
- }
- public struct Name
- {
- public string FirstName
- {
- get
- {
- throw new System.NotImplementedException();
- }
- set
- {
- }
- }
- public string LastName
- {
- get
- {
- throw new System.NotImplementedException();
- }
- set
- {
- }
- }
- }
- public abstract class Employee : IEmployee
- {
- #region IEmployee Members
- public int Age
- {
- get
- {
- throw new Exception(
- "The method or operation is not implemented.");
- }
- set
- {
- throw new Exception(
- "The method or operation is not implemented.");
- }
- }
- public Name Fullname
- {
- get
- {
- throw new Exception(
- "The method or operation is not implemented.");
- }
- set
- {
- throw new Exception(
- "The method or operation is not implemented.");
- }
- }
- public string EmployeeInfo()
- {
- throw new Exception(
- "The method or operation is not implemented.");
- }
- #endregion
- public abstract decimal CalculatePay();
- }
- public class HourlyEmployee : Employee
- {
- public decimal HourlyRate
- {
- get
- {
- throw new System.NotImplementedException();
- }
- set
- {
- }
- }
- public override decimal CalculatePay()
- {
- throw new Exception(
- "The method or operation is not implemented.");
- }
- public void Pay(decimal Hours)
- {
- throw new System.NotImplementedException();
- }
- }