-4

方法没有重载。我究竟做错了什么?我的头已经疼了:)

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

}

任何帮助将不胜感激:)

4

3 回答 3

0

静态方法不需要类的实例。它们在编译时静态解析,而不是像实例方法那样动态解析。从方法定义中删除静态或以正确的方式调用它们

于 2013-08-10T07:36:44.117 回答
0

您已将所有方法标记为static并尝试invoke使用Employee instance.

Remove static如果它们应该是instance方法,则来自 Employee 类的所有方法定义。

此外,您的方法需要您没有传递的参数。你应该考虑他们making fields/properties in your Employee class

开始阅读更多关于C#中的类和使用 C# 中的类的基本信息

于 2013-08-10T06:58:46.047 回答
0
Employee yuki = new Employee();
yuki.GrossPay();

更确切地说:

Employee.GrossPay();

您试图以错误的方式使用静态方法。

于 2013-08-10T07:02:19.410 回答