方法无重载。我究竟做错了什么? 我的脑袋疼得很疼:)
//this is the first class named Employee
namespace lala
{
public class Employee
{
public static double GrossPay(double WeeklySales) //grosspay
{
return WeeklySales * .07;
}
public static double FedTaxPaid(double GrossPay)
{
return GrossPay * .18;
}
public static double RetirementPaid(double GrossPay)
{
return GrossPay * .1;
}
public static double SocSecPaid(double GrossPay)
{
return GrossPay * .06;
}
public static double TotalDeductions(double SocSecPaid, double RetirementPaid, double FedTaxPaid)
{
return SocSecPaid + RetirementPaid + FedTaxPaid;
}
public static double TakeHomePay(double GrossPay, double TotalDeductions)
{
return GrossPay - TotalDeductions;
}
}
}
这是名为EmployeeApp的第二个类 这是我不知道为什么我的程序不起作用的地方
namespace lala
{
public class EmployeeApp
{
public static string name;
public static double WeeklySales;
public static void Main()
{
Employee yuki = new Employee();
GetInfo();
Console.WriteLine();
Console.WriteLine("Name: {0}", name);
Console.WriteLine();
Console.WriteLine("Gross Pay : {0}", yuki.GrossPay());
Console.WriteLine("Federal Tax Paid : {0}", yuki.FedTaxPaid());
Console.WriteLine("Social Security Paid : {0}", yuki.SocSecPaid());
Console.WriteLine("Retirement Paid : {0}", yuki.RetirementPaid());
Console.WriteLine("Total Deductions : {0}", yuki.TotalDeductions());
Console.WriteLine();
Console.WriteLine("Take-Home Pay : {0}", yuki.TakeHomePay());
Console.ReadKey();
}
public static string GetInfo()
{
Console.Write("Enter Employee Name : ");
name = Console.ReadLine();
Console.Write("Enter your Weekly Sales : ");
WeeklySales = Convert.ToDouble(Console.ReadLine());
return name;
}
}
}
任何帮助都会很高兴得到赞赏:)
答案 0 :(得分:0)
您已将所有方法标记为static
并尝试invoke
使用Employee instance
。
Remove static
方法,那么来自Employee类的所有方法定义的 instance
。
此外,您的方法需要您未传递的参数。你应该考虑它们making fields/properties in your Employee class
。
开始在Classes in C#和Basic of using classes in C#上阅读更多内容。
答案 1 :(得分:0)
Employee yuki = new Employee();
yuki.GrossPay();
或者更确切地说:
Employee.GrossPay();
您试图以错误的方式使用静态方法。
答案 2 :(得分:0)
静态方法不需要类的实例。它们在编译时静态解析,而不是像实例方法那样动态解析。从方法定义中移除静态或以正确的方式调用它们