简体   繁体   English

错误1类型“ Pay”已经使用相同的参数类型定义了一个名为“ ComputePay”的成员

[英]Error 1 Type 'Pay' already defines a member called 'ComputePay' with the same parameter types

I can't seem to figure our what the issue is here. 我似乎无法弄清楚问题出在哪里。 pph and with both equal to different values in the different overloads. pph和两者在不同的重载中等于不同的值。 I'm not sure what I'm doing wrong. 我不确定自己在做什么错。 I don't see how the values are the same. 我看不到值如何相同。

public class Pay
{
    public double ComputePay(double h,double pph,double with)
    {
        double net = 0;

        try
        {
            double gross = h * pph;
            net = gross - with;
        }
        catch (FormatException)
        {
            Console.WriteLine("Hour's cannot be less than zero");
        }

        return net;      
    }

    public double ComputePay(double h, double pph, double with = 0.15)
    {
        double net = 0;

        try
        {
            double gross = h * pph;
            net = gross - with;
        }
        catch (FormatException)
        {
            Console.WriteLine("Hour's cannot be less than zero");
        }

        return net;
    }

    public double ComputePay(double h, double pph = 5.85, double with = 0.15)
    {
        double net = 0;

        try
        {
            double gross = h * pph;
            net = gross - with;
        }
        catch (FormatException)
        {
            Console.WriteLine("Hour's cannot be less than zero");
        }

        return net;
    }
}

I'm not sure what I'm doing wrong. 我不确定自己在做什么错。

You've got three methods which both have three double parameters: 您有三个都有三个double参数的方法:

public double ComputePay(double h,double pph,double with)
public double ComputePay(double h, double pph, double with = 0.15)
public double ComputePay(double h, double pph = 5.85, double with = 0.15)

The fact that some of the parameters in some of the method declarations are optional is irrelevant to overloading here - you simply can't specify three methods like that. 一些方法声明中的某些参数是可选的,这一事实与此处的重载无关-您根本无法指定这样的三个方法。 Which method would you expect to be called if the caller specifies three arguments? 如果调用方指定了三个参数,则希望调用哪种方法?

Why do you want three methods anyway, given that they all do the same thing? 考虑到它们都执行相同的操作,为什么仍要使用三种方法? Just get rid of the first two. 只是摆脱前两个。

You cannot have two or more methods with the same signature. 您不能有两个或多个具有相同签名的方法。 This means that they cannot have the same name and parameter-types. 这意味着它们不能具有相同的名称和参数类型。 This has nothing to do with the value that will be passed to the method. 这与将传递给方法的值无关。

Correct could be: 正确的可能是:

public int Sum(int a, int b)
{
    return Sum(a, b, 0);
}

public int Sum(int a, int b, int c)
{
    return a + b + c;
}

Edit: 编辑:

Here's an interesting MSDN-article giving guidelines about Member Overloading . 这是一个有趣的MSDN文章,提供了有关Member Overloading的准则。

Your method signature (double, double, double) is the same. 您的方法签名(双精度,双精度,双精度)是相同的。 In this case, just delete the first two implementations. 在这种情况下,只需删除前两个实现即可。 The last one will most likely already behave the way you want. 最后一个很可能已经按照您想要的方式运行。

Your last two ComputePay (double, double, double) are the same. 您的最后两个ComputePay(double,double,double)是相同的。 Having a default variable doesn't make the method different. 拥有默认变量不会使方法有所不同。 Just use the second one and you'll be good to go. 只需使用第二个,就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型'Startup'已经定义了一个名为'Configuration'的成员,它具有相同的参数类型 - Type 'Startup' already defines a member called 'Configuration' with the same parameter types 错误 - 已经使用相同的参数类型定义了一个名为“InitializeComponent”的成员 - Error - already defines a member called 'InitializeComponent' with the same parameter types 错误已使用相同的参数类型定义了一个名为“索引”的成员 - Error already defines a member called 'Index' with the same parameter types C#错误:类型'x'已经定义了一个名为'y'的成员,它具有相同的参数类型 - C# error: Type 'x' already defines a member called 'y' with the same parameter types 我该如何修复错误:Type Form1'已经定义了一个名为'Dispose'的成员,它具有相同的参数类型? - How can i fix the error: Type Form1' already defines a member called 'Dispose' with the same parameter types? C#:专用模板方法-错误:类型“…”已经定义了具有相同参数类型的成员“…” - C#: specialized template method - Error: Type '…' already defines a member called '…' with the same parameter types 错误 CS0111:“程序”类型已经定义了一个名为“Main”的成员,具有相同的参数类型 c# - error CS0111: Type 'Program' already defines a member called 'Main with the same parameter types c# 已经使用相同的参数类型定义了一个名为“InitializeComponent”的成员 - Already defines a member called 'InitializeComponent' with the same parameter types 错误2类型'WindowsForms2012Snowman.WindowsForms2012SnowmanForm1'已经定义了具有相同参数类型的名为'Dispose'的成员 - Error 2 Type 'WindowsForms2012Snowman.WindowsForms2012SnowmanForm1' already defines a member called 'Dispose' with the same parameter types “'Form1' 已经定义了一个名为 '.ctor' 的成员,具有相同的参数类型” - "'Form1' already defines a member called '.ctor' with the same parameter types "
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM