为什么两个方法的返回类型不同,也不能用相同的签名声明? [英] Why can't two methods be declared with the same signature even though their return types are different?

查看:185
本文介绍了为什么两个方法的返回类型不同,也不能用相同的签名声明?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重复:按返回类型进行函数重载吗?

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

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.)

我是否缺少与OO设计有关的东西,这些东西使我正在尝试做OOP反模式?只要我明确告诉我要使用哪种方法,编译器肯定就能确定我要使用的方法.

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.

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

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();

但是即使在var的情况下,也应导致编译错误.

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. :-)

推荐答案

是因为强制类型.

假设您具有以下功能:

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.

现在,您已经标记了问题C#,但我发现设计一种可以满足您建议的语言没有错.一种可能性是将上述任何歧义命令标记为错误,并强制用户指定应调用的命令,例如使用cast:

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());

具体:

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

如果它不在标准流程中(微软会听的话),可能会添加到C#中,但是我认为将它添加到CC++的机会不大无需大量的努力.也许将它作为对gcc的扩展会更容易.

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.

这篇关于为什么两个方法的返回类型不同,也不能用相同的签名声明?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆