问题描述
我通过省略 totalFee 字段成功编译并运行了下面的源代码.如何将 totalFee 写入该程序,以便准确计算每项工作的总费用(费率 * 时间)?下面,你会看到我尝试使用一种方法;这产生了错误 CS0051(不一致的可访问性:参数类型 'Job' 比方法 'AddJobs.TotalPay(Job)' 更难访问).
I compiled and ran the source code below successfully by omitting the totalFee field. How do I write totalFee into this program so that it will accurately calculate the total fee for each job (rate * time)? Below, you'll see I tried using a method; which generated the error CS0051 (Inconsistent accessibility: parameter type 'Job' is less accessible than method 'AddJobs.TotalPay(Job)').
此源代码是对以下作业的响应:
This source code is in response to the following assignment:
为 Harold 的家庭服务设计一个 Job 类.该类包含四个数据字段——Job描述(例如,清洗窗户"),完成工作的时间(以小时为单位)(对于例如,3.5),为作业收取的每小时费率(例如,25.00 美元)和总费用工作(每小时费率乘以小时数).包括获取和设置每个字段的属性,除了总费用 - 该字段将是只读的,并且每次都会计算其值小时费或小时数已设置. 重载 + 运算符,以便两个作业可以添加.两个 Job 的总和是一个包含两个 Job 描述的新 Job原始作业(由和"连接),原始作业的时间总和,以及原始作业的平均每小时费率.编写一个 Main() 函数来演示所有方法都能正常工作.将文件另存为 DemoJobs.cs."
"Design a Job class for Harold’s Home Services. The class contains four data fields—Job description (for example, "wash windows"), time in hours to complete the Job (for example, 3.5), per-hour rate charged for the Job (for example, $25.00), and total fee for the Job (hourly rate times hours). Include properties to get and set each field except the total fee—that field will be read-only, and its value is calculated each time either the hourly fee or the number of hours is set. Overload the + operator so that two Jobs can be added. The sum of two Jobs is a new Job containing the descriptions of both original Jobs ( joined by "and"), the sum of the time in hours for the original Jobs, and the average of the hourly rate for the original Jobs. Write a Main()function that demonstrates all the methods work correctly. Save the file as DemoJobs.cs."
Microsoft® Visual C#® 2008,面向对象编程简介,3e,Joyce Farrell
Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell
这里是源代码:
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}
Total time needed to complete the job: {1} hours
Hourly fee: {2} per hour", job1.Description, job1.Time, job1.Rate.ToString("C"));
TotalPay(job1);
Console.WriteLine("The second job's description: {0}
Total time needed to complete the job: {1} hours
Hourly fee: {2} per hour", job2.Description, job2.Time, job2.Rate.ToString("C"));
TotalPay(job2);
Console.WriteLine("The third job's description: {0}
Total time needed to complete the job: {1} hours
Hourly 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;}
}
推荐答案
你没有为你的类指定可见性修饰符,这使它成为内部的.
You haven't specified a visibility modifier for your class, which makes it internal.
尝试更改此行:
class Job
到这里:
public class Job
这篇关于错误 CS0051(不一致的可访问性:参数类型 'Job' 的可访问性低于方法 'AddJobs.TotalPay(Job)')的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!