public class A
{
public virtual string Go(string str) { return str; }
}
public class B : A
{
public override string Go(string str) {return base.Go(str);}
public string Go(IList<string> list) {return "list";}
}
public static void Main(string[] args)
{
var ob = new B();
Console.WriteLine(ob.Go(null));
}
Run Code Online (Sandbox Code Playgroud)
http://dotnetpad.net/ViewPaste/s6VZDImprk2_CqulFcDJ1A
如果我运行这个程序,我会将"list"发送到输出.为什么这不会在编译器中触发模糊的引用错误?
Die*_*hon 15
由于在B中没有定义带字符串的重载(仅覆盖),因此它的优先级低于采用字符串的优先级IList<string>.
因此,第二次超载获胜并且没有歧义.
这在http://csharpindepth.com/Articles/General/Overloading.aspx中有详细解释
| 归档时间: |
|
| 查看次数: |
241 次 |
| 最近记录: |