错误的重载决议? [英] Wrong overload resolution ?

查看:52
本文介绍了错误的重载决议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


请查看下面的代码并尝试介入其中。编译后的代码调用松散类型的方法public void Method1(object o)!?!?


我是否正确C#编译器执行了错误的重载决策?


我在这里使用了object和string类型的参数,只是为了说明问题。真的,我有一个更深的继承图,如果强类型的重载就像覆盖public void Method1(BaseType x),事情变得更有趣。当我用SubType类型的参数(继承BaseType)调用它时,会调用正确的方法。


感谢任何有用的点。


Regadrs,

Vladimir Granitsky

using System; using System.Diagnostics; namespace OverloadResolution {public class Class1 {virtual public void Method1(string s){Trace.WriteLine(" Class1.Method1"); public class Class2:Class1 {override public void Method1(string s){Trace.WriteLine(" Class2.Method1a"); public void Method1(object o){Trace.WriteLine(" Class2.Method1b"); class Client {[STAThread] static void Main(string [] args){string s =" blah" ;; Class2 o2 = new Class2(); o2.Method1(一个或多个);

Hi guys,

Please, look at the code below and try to step into it. The compiled code calls the loosely typed method public void Method1(object o) !?!?

Am I right that C# compiler does wrong overload resolution ?

I''ve used parameters of type object and string here, just to illustrate the problem. Really I have a bit more deep inheritance graph, and the things get more interesting if the strongly typed overload is like override public void Method1(BaseType x). When I call it with parameter of type SubType (that inherits BaseType) the right method is called.

Thanks for any useful points.

Regadrs,
Vladimir Granitsky
using System;using System.Diagnostics;namespace OverloadResolution{ public class Class1 { virtual public void Method1(string s) { Trace.WriteLine("Class1.Method1"); } } public class Class2 : Class1 { override public void Method1(string s) { Trace.WriteLine("Class2.Method1a"); } public void Method1(object o) { Trace.WriteLine("Class2.Method1b"); } } class Client { [STAThread] static void Main(string[] args) { string s = "blah"; Class2 o2 = new Class2(); o2.Method1(s); } }}

推荐答案

您好Vladimir,


我使用.NET Framework 1.0具有相同的功能;错"结果!


仅更改Main:

static void Main(string [] args)

{

string s =" blah";

Class1 o2 = new Class2();

o2.Method1(s);

}


或在Class1中添加方法,以及覆盖它在Class2中


public virtual void Method1(object o){

Console.WriteLine(" Class1.Method1(object)");

}


给了我正确的权利。结果......


看起来覆盖方法的优先级较低。超过

a超重的非重写方法。

当我替换Class2的方法时:

new public void Method1(string s)

{

Trace.WriteLine(" Class2.Method1(string)");

}

then超载使用了它...


奇怪且令人毛骨悚然:-(


Marcin
Hi Vladimir,

I work with .NET Framework 1.0 has the same "wrong" result!

Only change in "Main":
static void Main(string[] args)
{
string s = "blah";
Class1 o2 = new Class2();
o2.Method1(s);
}

or adding method into Class1, and "override" it in Class2

public virtual void Method1(object o) {
Console.WriteLine("Class1.Method1(object)");
}

gives me "right" result...

It looks like an overriden method has less "priority" than
a non-overriden methods in case of overload.
When i replaced method of Class2:
new public void Method1(string s)
{
Trace.WriteLine("Class2.Method1(string)");
}
then it was used by overload...

Strange and creepy :-(

Marcin
大家好,

请看下面的代码并尝试进入它。编译的代码调用松散类型的方法public void Method1(object o)!?!?<我是对的,C#编译器做了错误的重载解析吗?

我在这里使用了object和string类型的参数,只是为了说明这个问题。真的我有一个更深的继承图,如果强类型的重载像覆盖public void Method1(BaseType x)那样,事情变得更有趣。当我用
参数调用它时键入SubType(继承BaseType)righ调用方法。

感谢任何有用的点。

Regadrs,
Vladimir Granitsky

使用System;
使用System.Diagnostics;
命名空间OverloadResolution
公共类Class1
{
虚拟公共void Method1(字符串s)
{< br。> Trace.WriteLine(" Class1.Method1");
}
}

公共类Class2:Class1
{
覆盖public void Method1(string s)
{
Trace.WriteLine(" Class2.Method1a");
}

public void Method1(object o)
{
Trace.WriteLine(" Class2.Method1b");
}
}
类客户
{
[STAThread]
static void Main(string [] args)
{
string s =" blah";
Class2 o2 = new Class2(); <
o2.Method1(s);
}
}
}
Hi guys,

Please, look at the code below and try to step into it. The compiled
code calls the loosely typed method public void Method1(object o) !?!?

Am I right that C# compiler does wrong overload resolution ?

I''ve used parameters of type object and string here, just to illustrate
the problem. Really I have a bit more deep inheritance graph, and the
things get more interesting if the strongly typed overload is
like override public void Method1(BaseType x). When I call it with
parameter of type SubType (that inherits BaseType) the right method is
called.

Thanks for any useful points.

Regadrs,
Vladimir Granitsky

using System;
using System.Diagnostics;
namespace OverloadResolution
{
public class Class1
{
virtual public void Method1(string s)
{
Trace.WriteLine("Class1.Method1");
}
}

public class Class2 : Class1
{
override public void Method1(string s)
{
Trace.WriteLine("Class2.Method1a");
}

public void Method1(object o)
{
Trace.WriteLine("Class2.Method1b");
}
}

class Client
{
[STAThread]
static void Main(string[] args)
{
string s = "blah";
Class2 o2 = new Class2();
o2.Method1(s);
}
}
}



>我是对的,C#编译器做错了重载决定吗?


编号

如果你引入一个重载方法的新定义那么就有了

a具体要做的事我现在不记得了,但它是

记录的。


-

- ---
http://michael.moreno.free.fr/

> Am I right that C# compiler does wrong overload resolution ?

No.
If you introduce a new definition of an overloaded method then there is
a specific thing to do I cannot remember of right now but it is
documented.

--
----
http://michael.moreno.free.fr/


嗯,我猜它是按照C#规范工作的,但

我倾向于在规范编写者身上做出相当糟糕的设计选择。


要了解正在进行的操作,请添加以下行:

Class1 o1 = $ 2;

o1.Method1(s);


到Main()函数的末尾。运行它,你会注意到,当

o2.Method1()给出错误的响应,o1.Method1()是正确的。


现在,替换覆盖与新一起(或者只是删除它)。现在,

o2.Method1()是正确的,而o1.Method1()是错误的。


所以,我想到的是Method1( object)隐藏了

Class1.Method1()(包括隐藏它的覆盖)。

但是,现在将Method1(对象o)更改为Method1(int o )。现在,两个o2.Method1()

& o1.Method1()是正确的。因此,如果参数类似于
,它只会隐藏它(C ++会根据名称隐藏它)

" Vladimir Granitsky" < VL ********** @ hotmail.com>在消息中写道

新闻:## ************** @ TK2MSFTNGP09.phx.gbl ...

大家好,


请查看下面的代码并尝试进入它。编译后的代码

调用松散类型的方法public void Method1(object o)!?!?


我是否正确C#编译器执行错误的重载解析?


我在这里使用了object和string类型的参数,只是为了说明

问题。真的,我有一个更深的继承图,如果强类型重载像覆盖public void

方法1(BaseType x),那么事情会更有趣。当我使用SubType类型的参数调用它(

继承BaseType)时,会调用正确的方法。


感谢任何有用的点。


Regadrs,

Vladimir Granitsky

使用System;使用System.Diagnostics;名称空间OverloadResolution {

公共类Class1 {virtual public void Method1(string s)

{Trace.WriteLine(" Class1.Method1");公共

class Class2:Class1 {override public void Method1(string s)

{Trace.WriteLine(" Class2.Method1a"); } public void

Method1(对象o){

Trace.WriteLine(" Class2.Method1b"); class client

{[STAThread] static void Main(string [] args)

{string s =" blah" ;; Class2 o2 = new Class2();

o2.Method1(s); }
Well, I''m gonna guess that it''s working according to the C# spec, but
I''m leaning toward it being a rather bad design choice on the spec writers.

To follow what going on, add the lines:
Class1 o1 = o2;
o1.Method1(s);

to the end of your Main() function. Run it, and you''ll note that while
o2.Method1() give the wrong response, o1.Method1() is correct.

Now, replace the "override" with "new" (or just delete it). Now,
o2.Method1() is correct, while o1.Method1() is wrong.

So, what I THOUGHT was happening was that Method1(object) was hiding the
Class1.Method1() (including hiding it''s override).
BUT, now change Method1(object o) to Method1(int o). Now, both o2.Method1()
& o1.Method1() are correct. So, it''s only hiding it if the parameters are
similar (C++ would hide it based on just the name)
"Vladimir Granitsky" <vl**********@hotmail.com> wrote in message
news:##**************@TK2MSFTNGP09.phx.gbl...
Hi guys,

Please, look at the code below and try to step into it. The compiled code
calls the loosely typed method public void Method1(object o) !?!?

Am I right that C# compiler does wrong overload resolution ?

I''ve used parameters of type object and string here, just to illustrate the
problem. Really I have a bit more deep inheritance graph, and the things get
more interesting if the strongly typed overload is like override public void
Method1(BaseType x). When I call it with parameter of type SubType (that
inherits BaseType) the right method is called.

Thanks for any useful points.

Regadrs,
Vladimir Granitsky
using System;using System.Diagnostics;namespace OverloadResolution{
public class Class1 { virtual public void Method1(string s)
{ Trace.WriteLine("Class1.Method1"); } } public
class Class2 : Class1 { override public void Method1(string s)
{ Trace.WriteLine("Class2.Method1a"); } public void
Method1(object o) {
Trace.WriteLine("Class2.Method1b"); } } class Client
{ [STAThread] static void Main(string[] args)
{ string s = "blah"; Class2 o2 = new Class2();
o2.Method1(s); } }}


这篇关于错误的重载决议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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