为什么两个方法不能用相同的签名声明,即使它们的返回类型不同?

发布于 2024-07-13 17:49:37 字数 932 浏览 10 评论 0原文

重复按返回类型重载函数?


也许这是这是一个非常愚蠢的问题,但我不明白为什么我不能声明两个具有相同签名的方法,当它们具有不同的返回类型时。

public class MyClass
{
    private double d = 0;

    public double MyMethod()
    {
        return d;
    }

    public string MyMethod()
    {
        return d.ToString();
    }
}

我收到一个编译错误,指出该类已经定义了具有相同参数类型的成员。

(显然,我在代码中使用它的方式并不像我的示例代码那么简单......但我认为它传达了这个想法。)

我是否遗漏了一些关于 OO 设计的东西,这些东西使得我想要做的事情OOP 反模式? 当然,只要我明确告诉编译器我想要哪种方法,编译器就应该能够确定我正在尝试使用哪种方法。

给定 MyClass myClass = new MyClass(); 我希望以下代码能够工作:

double d = myClass.MyMethod();
string s = myClass.MyMethod();

我希望以下代码有问题:

var v = myClass.MyMethod();

但即使在 var 的情况下,它应该会导致编译错误。

有人能看到我在这里做错了什么吗? 我非常高兴得到纠正。 :-)

Duplicate: Function overloading by return type?


Maybe this is a very silly question but I don't understand why I can't declare two methods that have the same signature when they have different return types.

public class MyClass
{
    private double d = 0;

    public double MyMethod()
    {
        return d;
    }

    public string MyMethod()
    {
        return d.ToString();
    }
}

I get a compile error that states that the class already defines a member with the same parameter types.

(Obviously the way I'm using this in my code isn't as simple as my example code...but I think it gets the idea across.)

Am I missing something concerning OO design that makes what I'm trying to do an OOP anti-pattern? Surely the compiler should be able to determine which method I'm trying to use as long as I specifically tell it which one I want.

Given MyClass myClass = new MyClass(); I would expect the following code to work:

double d = myClass.MyMethod();
string s = myClass.MyMethod();

I would expect the following code to have problems:

var v = myClass.MyMethod();

But even in the case of var it should result in a compile error.

Can anyone see what I'm doing wrong here? I'm more than happy to be corrected. :-)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

樱花坊 2024-07-20 17:49:38

方法签名仅是名称和输入参数(类型和顺序)。 返回类型不是签名的一部分。 因此,具有相同名称和输入参数的两个方法是相同的并且彼此冲突。

A method signature is the name and input parameters (type & order) only. The return type is not part of the signature. Thus two methods with the same name and input parameters are identical and conflict with each other.

没企图 2024-07-20 17:49:38

您将方法分配给什么变量(如果有)无法通知编译器要使用哪种方法。 想象一下:

String message = "Result = " + myClass.MyMethod();

What, if any, variable you're assigning the method to can't inform the compiler which method to use. Imagine:

String message = "Result = " + myClass.MyMethod();

彼岸花ソ最美的依靠 2024-07-20 17:49:37

这是因为类型强制。

假设您有以下函数:

int x(double);
float x(double);

double y = x(1.0);

现在,您应该调用这两个原型中的哪一个,特别是当它们执行两个完全不同的操作时?

基本上,在语言设计的早期就决定只使用函数名称和参数来决定调用哪个实际函数,并且我们一直坚持这一点,直到新标准出现。

现在,您已将您的问题标记为C#,但我认为设计一种可以执行您建议的语言没有任何问题。 一种可能性是将任何不明确的命令标记为错误,例如上面的命令,并强制用户指定应该调用哪个命令,例如使用强制转换:

int x(double);
float x(double);
double y = (float)(x(1.0));    // overload casting
double y = float:x(1.0);       // or use new syntax (looks nicer, IMNSHO)

这可以允许编译器选择正确的版本。 这甚至适用于其他答案提出的一些问题。 您可以将模棱两可的:转换

System.out.Println(myClass.MyMethod());

为具体的:

System.out.Println(string:myClass.MyMethod());

如果它距离标准流程不太远(并且微软会倾听),那么这可能会被添加到C#中,但我不太看重您的有机会将其添加到 CC++ 中,而无需付出巨大的努力。 也许将其作为 gcc 的扩展会更容易。

It's because of type coercion.

Say you have the following functions:

int x(double);
float x(double);

double y = x(1.0);

Now, which of the two prototypes should you call, especially if they do two totally different things?

Basically, a decision was made early on in the language design to only use the function name and arguments to decide which actual function gets called, and we're stuck with that until a new standard arrives.

Now, you've tagged your question C# but I see nothing wrong with designing a language that can do what you suggest. One possibility would be to flag as an error any ambiguous commands like above and force the user to specify which should be called, such as with casting:

int x(double);
float x(double);
double y = (float)(x(1.0));    // overload casting
double y = float:x(1.0);       // or use new syntax (looks nicer, IMNSHO)

This could allow the compiler to choose the right version. This would even work for some issues that other answers have raised. You could turn the ambiguous:

System.out.Println(myClass.MyMethod());

into the specific:

System.out.Println(string:myClass.MyMethod());

This may be possible to get added to C# if it's not too far into a standards process (and Microsoft will listen) but I don't think much of your chances getting it added to C or C++ without an awfully large amount of effort. Maybe doing it as an extension to gcc would be easier.

-黛色若梦 2024-07-20 17:49:37

在不“捕获”返回类型的情况下,没有什么可以阻止您调用方法。 没有什么可以阻止您这样做:

myClass.MyMethod();

编译器如何知道在这种情况下调用哪一个?

编辑:补充一下,在 C# 3.0 中,当您可以使用 var 时,编译器如何知道您在执行此操作时正在调用哪个方法:

var result = myClass.MyMethod();

There's nothing from stopping you from calling your method without "capturing" the return type. There's nothing from stopping you from doing this:

myClass.MyMethod();

How will the compiler know which one to call in that case?

Edit: Adding to that, in C# 3.0, when you can use var, how will the compiler know which method you're calling when you do this:

var result = myClass.MyMethod();
双马尾 2024-07-20 17:49:37

因为调用方法时返回类型并不那么重要。 在许多情况下,只有返回类型不同的方法会导致歧义。 您可能根本不将结果存储在变量中,或者执行类似以下操作:

System.out.Println(myClass.MyMethod());

编译器无法确定您想要调用哪个方法。

Because the return type isn't all that important when calling a method. It would lead to ambiguity in many cases to have methods only differing by return type. You might not store the result in a variable at all, or do something like this:

System.out.Println(myClass.MyMethod());

The compiler would have no way to figure out, which of the methods you wanted to call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文