My source code (below) is generating Error CS0563 because both of the parameters in my CombinedJobs operator+ (see "//Step 5: ..." in source code) are listed as Job (as opposed to int, double, etc). How can I change this code to eliminate this error? This code is in response to the assignment listed below.
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."
From: Microsoft® Visual C#® 2008, An Introduction to Object-Oriented Programming, 3e, Joyce Farrell
Source Code:
using System;
//Step 1: Creating the Job class and designating its fields
public class Job
{
private string jobDescription;
private int jobTotalTime;
private double jobRate;
private double jobTotalFee;
//Step 2: Using the 'this' object will allow me to designate values for
//"this" instantiation of an object later in the program
public string JobDescription
{
get
{
return this.jobDescription;
}
set
{
this.jobDescription = value;
}
}
public int JobTotalTime
{
get
{
return this.jobTotalTime;
}
set
{
this.jobTotalTime = value;
}
}
public double JobRate
{
get
{
开发者_JAVA百科 return this.jobRate;
}
set
{
this.jobRate = value;
}
}
public double JobTotalFee
{
get
{
return this.jobTotalFee;
}
set
{
this.jobTotalFee = this.JobRate * this.JobTotalTime;
}
}
//Step 3: Creating a method will allow me to display the different jobs
//with their descriptions and other variable fields later in the program
public void JobMessage()
{
Console.WriteLine("Job description: {0} /nTotal time needed to complete the job: {1} hours /nHourly fee: ${2} /nTotal fee for the job is: ${3}", this.JobDescription, this.JobTotalTime, this.JobRate, this.JobTotalFee);
}
}
//Step 4: Creating two instantiations of the Job Class
public class CreateTwoJobs
{
public static void Main()
{
Job jobA = new Job();
Job jobB = new Job();
jobA.JobDescription = "washing windows";
jobA.JobTotalTime = 5;
jobA.JobRate = 25.00;
jobB.JobDescription = "walking dogs";
jobB.JobTotalTime = 10;
jobB.JobRate = 11.00;
jobA.JobMessage();
jobB.JobMessage();
}
}
//Step 5: Creating a third instantiation of the Job Class that is a combination
//of the first two instantiations using the overloaded + operator
public class CombinedJobs
{
public CombinedJobs(string jobDescription, int jobTotalTime, double jobRate, double jobTotalFee)
{
JobDescription = jobDescription;
JobTotalTime = jobTotalTime;
JobRate = jobRate;
JobTotalFee = jobTotalFee;
}
public static CombinedJobs operator+(Job jobA, Job jobB)
{
string newDescription = jobA.JobDescription + " and " + jobB.JobDescription;
int newTotalTime = jobA.JobTotalTime + jobB.JobTotalTime;
double newJobRate = (jobA.JobRate + jobB.JobRate) / 2;
double newTotalFee = newJobRate * newTotalTime
}
public void CombinedJobMessage()
{
Console.WriteLine("Job description: {0} /nTotal time needed to complete the job: {1} hours /nHourly fee: ${2} /nTotal fee for the job is: ${3}", newDescription, newTotalTime, newJobRate, newTotalTime);
}
public static void Main()
{
CombinedJobMessage();
}
}
The operator+
method has to be inside the class of one of its parameters. Since in your case both parameters are Job
, the method has to be inside the Job
class. It can't be inside CombinedJobs
.
Also, you declared the operator as returning CombinedJobs
, so that means you have to actually return it. This is not causing the error you are describing, but it would cause another compile error.
精彩评论