我有这些课程:
- 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时,有很多方法可以做你需要做很多工作.