Csharp/C#教程:为什么不调用重载方法?分享


为什么不调用重载方法?

我认为被调用的方法是运行时决定的,还是我错过了什么? 示例代码:

class Program { static void Main(string[] args) { var magic = new MagicClass(); magic.DoStuff(new ImplA()); magic.DoStuff(new ImplB()); Console.ReadLine(); } } class MagicClass { internal void DoStuff(T input) where T : SomeBase { HiThere(input); } void HiThere(SomeBase input) { Console.WriteLine("Base impl"); } void HiThere(ImplA input) { Console.WriteLine("ImplA"); } void HiThere(ImplB input) { Console.WriteLine("ImplB"); } } abstract class SomeBase { } class ImplA : SomeBase{} class ImplB : SomeBase{} 

我以为我会得到:

 ImplA ImplB 

作为输出但它打印Base impl 。 有什么我可以做的,以获得重载方法而不输入输入?

编译器选择过载。 对于这里的电话:

 internal void DoStuff(T input) where T : SomeBase { HiThere(input); } 

它选择了SomeBase ,因为这就是编译时的全部内容。

你最想要的是覆盖。 这意味着必须将不同的逻辑放入SomeBase的inheritance者中:

 abstract class SomeBase { abstract string Name { get; } } class ImplA : SomeBase{ override string Name { get { return "ImplA"; } } } class ImplB : SomeBase{ override string Name { get { return "ImplB"; } } } void HiThere(SomeBase input) { Console.WriteLine(input.Name); } 

在编译期间选择过载。
在运行时选择覆盖。

在这里,编译器只知道T可以分配给SomeBase ,但没有别的。 实际上,如果它按预期工作,你将能够完全跳过where T : SomeBase部分。 您需要它的原因是编译器需要知道该信息,以便检查在提供的对象上可以调用的内容。

上述就是C#学习教程:为什么不调用重载方法?分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/952368.html

(0)
上一篇 2021年11月19日
下一篇 2021年11月19日

精彩推荐