错误 CS0051(可访问性不一致:参数类型“Job”的访问性不如方法“AddJobs.TotalPay(Job)”)

发布于 2024-09-29 18:06:03 字数 2429 浏览 0 评论 0原文

我通过省略totalFee字段成功编译并运行了下面的源代码。如何将totalFee写入该程序,以便它准确计算每项工作的总费用(费率*时间)?下面,你会看到我尝试使用一种方法;生成错误 CS0051(可访问性不一致:参数类型“Job”比方法“AddJobs.TotalPay(Job)”更难访问)。

该源代码是为了响应以下作业:

“为 Harold 的家庭服务设计一个 Job 类。该类包含四个数据字段 — Job 描述(例如,“清洗窗户”)、完成作业的时间(例如 例如,3.5)、该工作的每小时费率(例如,25.00 美元)以及总费用 工作(每小时费率乘以小时数)。 包含用于获取和设置每个字段的属性,除了 总费用 - 该字段将是只读的,并且每次都会计算其值 设置每小时费用或小时数。 重载 + 运算符,以便两个作业 可以添加。两个作业的总和是一个新作业,包含两个作业的描述 原始作业(由“和”连接),原始作业的时间总和(以小时为单位),以及 原始职位的平均每小时工资。编写一个 Main() 函数来演示所有方法都能正常工作。将文件另存为 DemoJobs.cs。”

Microsoft® Visual C#® 2008,面向对象编程简介,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} \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;}
}

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:

"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, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell

Here is the source code:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你又不是我 2024-10-06 18:06:03

您尚未为您的类指定可见性修饰符,这使其成为内部类。

尝试将此行:更改

class Job

为:

public class Job

You haven't specified a visibility modifier for your class, which makes it internal.

Try changing this line:

class Job

to this:

public class Job
少女七分熟 2024-10-06 18:06:03

这意味着您正在访问一个非公开的类。像这样公开该类:

public class Job
{

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

     Time = time;

     Rate = rate;
  }

It means you are accessing a class that is not public. Make the class public like this:

public class Job
{

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

     Time = time;

     Rate = rate;
  }
残龙傲雪 2024-10-06 18:06:03

Lasse V. Karlsen 是对的,您确实需要在 Job 类的前面添加一个公共访问修饰符。忽略方法点“A”显示语法错误的情况,表明该人正在尝试访问私有字段。当人们应该尝试访问该字段的属性而不是访问私有字段时。

公共静态无效TotalPay(作业方法)
{
答: 答:
double 总费用 = Job.rate * Job.time;
Console.WriteLine("总费用为:{0}", TotalFee.ToString("C"));
}
有几个不同

的地方有类似的错误,只是遵循所有“A”的使用系统;

公共类添加作业
{
私人双总费用;

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

}

You are right Lasse V. Karlsen you do need to add a public access modifier to the front of the class Job. Over looking the case that a method point "A" is showing the syntax error that indicates that the person was trying to access the private field. When the person should of tried to access the property of the field instead of accessing a field that was a private field.

public static void TotalPay(Job method)
{
A: A:
double totalFee = Job.rate * Job.time;
Console.WriteLine("The total fee is: {0}", TotalFee.ToString("C"));
}
}

There were several different place that had similar errors just follow all of the "A"'s using System;

public class AddJobs
{
private double 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 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; }

}

献世佛 2024-10-06 18:06:03
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; }
}
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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文