2010-10-30 66 views
10

我通過忽略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;} 
} 
+0

嗨,歡迎來到Stack Overflow。一個友好的提示,使用工具欄上的010101鍵選擇所有代碼後縮進四個空格,這樣即使聲明被格式化爲代碼。 – 2010-10-30 21:13:25

回答

22

您還沒有指定可見性改性劑類,這使得它的內部

嘗試改變這一行:

class Job 

這樣:

public class Job 
0
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 = method.Rate * method.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; } 
} 
1

你是正確的Lasse五卡爾森你需要一個公共訪問修飾符添加到類工作的前面。在查看方法指向「A」的情況下,顯示語法錯誤,指示該人試圖訪問專用字段。當這個人應該試圖訪問該領域的財產而不是訪問一個私人領域的領域。

public static void TotalPay(作業方法) 答案:A: double totalFee = Job.rate * Job.time; Console.WriteLine(「總費用:{0}」,TotalFee。的ToString( 「C」)); } }

有幾個不同的地方有類似的錯誤只是按照所有的「A」使用系統;

public class AddJobs { private private totalFee;

    //A: 
public AddJobs(double TotalFee) 
{ //A: 
    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 = method.Rate * method.Time; 
    Console.WriteLine("The total fee is: {0}", totalFee.ToString("C")); 
} 

}

公共類職位 {

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; } 

}

+0

歡迎來到Stackoverflow。你介意清理你答案的格式嗎? – Henrik 2012-11-29 12:13:04

3

這意味着u的訪問類非公共..使課堂公衆這樣

public class Job 
{ 

    public Job(string description, double time, double rate) 
    { 
    Description = description; 

    Time = time; 

    Rate = rate; 
    } 
相關問題