方法无过载.我做错了什么

本文关键字:错了 什么 方法 | 更新日期: 2023-09-27 18:27:12

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

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

}

任何帮助将不胜感激:)

方法无过载.我做错了什么

Employee yuki = new Employee();
yuki.GrossPay();

或者更确切地说:

Employee.GrossPay();

您正在尝试以错误的方式使用静态方法。

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

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

此外,您的方法需要您未传递的参数。您应该将它们视为making fields/properties in your Employee class.

开始阅读有关 C# 中的类和使用 C# 中的类的基础知识的更多信息。

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