
class Asset{ }class House:Asset{ } 考虑这些局外人的静态功能:
static voID Foo (Asset a) { }static voID Foo (House h) { } 如果我写:
House h = new House (...); Foo(h);
它将调用Foo(House)(编译时绑定)
如果我写:
Asset a = new House (...);Foo(a);
它会调用Foo(Asset)(编译时绑定)
目标:访问运行时类型方法:
我有2个选择:
1)使用动态如下:
Asset a = new House (...); Foo ((dynamic)a); // Now it will call Foo(House)
2)使用多态机制将函数从static移动到overrIDe.
题 :
有没有其他方法这样做(没有将函数移动到多态机制||动态)?
解决方法goal : access the runtime type method
这就是动态关键字的用途.它实际上是一个非常干净的&快速做多次派遣的方法.
您对Multiple Dispatch的最终选择是
>动态
> Double dispatch虚拟方法
>一些哈希匿名函数规则集合
> if(x是House)……否则if(x是资产)……
>反思 – 真的很慢,也很难看
question : is there any other way of doing it ( without moving the functions to polymorphism mechanism || dynamic) ?
所以是的,当你可以使用动态快速,不易出错且真正的clean syntax wise时,有很多方法可以做你需要做很多工作.
总结以上是内存溢出为你收集整理的c#多个派遣选项?全部内容,希望文章能够帮你解决c#多个派遣选项?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)