我通過忽略totalFee字段成功編譯並運行了以下源代碼。如何將totalFee寫入此程序,以便準確計算每項工作的總費用(費率*時間)?下面,你會看到我嘗試使用一種方法;它產生了錯誤CS0051(不一致的可訪問性:參數類型'Job'比方法'AddJobs.TotalPay(Job)'更難以訪問)。錯誤CS0051(不一致的可訪問性:參數類型「作業」比方法'AddJobs.TotalPay(作業)'更難以訪問)
此源代碼是響應以下分配:
「。設計一個招聘類哈羅德的家庭服務類包含四個數據字段,工作 描述(例如,‘洗窗’) ,完成作業所需的時間(以 爲例,3.5),按工作收取的每小時費率(例如25.00美元)以及工作總費用 (小時費率小時數)包含要獲取的屬性並設置除 之外的每個字段的總費用 - 該字段將是隻讀的,並且其值每次被計算爲 小時費或小時數被設定。重載+運算符,以便可以添加兩個作業 。兩個作業的總和是一個新的作業,其中包含 原始作業(由「和」加起來)的描述,原始作業的時間總和以及原始作業的小時費率的平均值 。編寫一個Main()函數,演示所有方法的正確工作。將文件另存爲DemoJobs.cs「
微軟的Visual C#®2008年,介紹了面向對象編程,3E,喬伊斯·法雷爾
這裏是源代碼:
using System;
public class AddJobs
{
private double totalFee;
public AddJobs(double totalFee)
{
TotalFee = totalFee;
}
public static void Main()
{
Job job1 = new Job("washing windows", 5.00, 25.00);
Job job2 = new Job("walking a dog", 3.00, 11.00);
Job job3;
job3 = job1 + job2;
Console.WriteLine("The first job's description: {0} \nTotal time needed to complete the job: {1} hours \nHourly fee: {2} per hour", job1.Description, job1.Time, job1.Rate.ToString("C"));
TotalPay(job1);
Console.WriteLine("The second job's description: {0} \nTotal time needed to complete the job: {1} hours \nHourly fee: {2} per hour", job2.Description, job2.Time, job2.Rate.ToString("C"));
TotalPay(job2);
Console.WriteLine("The third job's description: {0} \nTotal time needed to complete the job: {1} hours \nHourly fee: {2} per hour", job3.Description, job3.Time, job3.Rate.ToString("C"));
TotalPay(job3);
}
public static void TotalPay(Job method)
{
double totalFee = Job.rate * Job.time;
Console.WriteLine("The total fee is: {0}", TotalFee.ToString("C"));
}
}
class Job
{
public Job(string description, double time, double rate)
{
Description = description;
Time = time;
Rate = rate;
}
public static Job operator+(Job first, Job second)
{
string newDescription = first.Description + " and " + second.Description;
double newTime = first.Time + second.Time;
double newRate = (first.Rate + second.Rate)/2;
double newTotalFee = newRate * newTime;
return(new Job(newDescription, newTime, newRate));
}
public string Description {get; set;}
public double Time {get; set;}
public double Rate {get; set;}
}
嗨,歡迎來到Stack Overflow。一個友好的提示,使用工具欄上的010101鍵選擇所有代碼後縮進四個空格,這樣即使聲明被格式化爲代碼。 – 2010-10-30 21:13:25