本文介绍了错误的重载决议?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 大家好, 请查看下面的代码并尝试介入其中。编译后的代码调用松散类型的方法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 Granitskyusing 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); } }}推荐答案 这篇关于错误的重载决议?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-10 05:05