事例講述Visual Studio Employee類
學(xué)習(xí)Visual Studio時(shí),你可能會(huì)遇到Visual Studio Employee類問題,Visual Studio Employee類的派生類型必須具有計(jì)算薪水操作,這里將介紹Visual Studio Employee類問題的解決方法,在這里拿出來和大家分享一下。
#t#為Visual Studio Employee類添加一個(gè)CalculatePay抽象方法,該方法返回一個(gè)十進(jìn)制數(shù)。在Visual Studio Employee類的標(biāo)題打開其快捷菜單,選擇“添加”子菜單和“方法”菜單命令,為新方法命名為“CalculatePay”。在“類詳細(xì)信息”窗口設(shè)置十進(jìn)制數(shù)作為返回類型。該方法應(yīng)該設(shè)置為抽象的。打開CalculatePay方法的快捷菜單并修改“繼承修飾符”為abstract(抽象)。然后系統(tǒng)顯示一個(gè)消息框要求你對(duì)這一修改進(jìn)行確認(rèn),選“確定”。
Hourly Employee類應(yīng)該繼承Visual Studio Employee類。這一關(guān)系用繼承線來創(chuàng)建,具體做法是:將繼承線從Hourly Employee類拖到Visual Studio Employee類。
***,為Hourly Employee類添加一個(gè)Pay方法。Pay方法只有一個(gè)參數(shù),就是工作的小時(shí)數(shù)。在“類詳細(xì)信息”窗口,擴(kuò)展Pay方法的行來打開 “添加參數(shù)”項(xiàng),選擇“添加參數(shù)”行并輸入“Hours”作為參數(shù)名,修改類型為十進(jìn)制數(shù)。添加一個(gè)HourlyRate屬性到該類以使其完整,該屬性也是十進(jìn)制數(shù)類型的。
以下代碼由類關(guān)系圖創(chuàng)建。創(chuàng)建的函數(shù)是很粗略的,接下來只需要實(shí)現(xiàn)這些粗略的函數(shù)。
- 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();
- }
- }