有没有办法在子类中覆盖opDispatch?我真正想做的是传递一个以超类作为静态类型的变量,但是将对opDispatch的调用重定向到它的子类型(动态类型).
基本上,我希望此代码打印"Sub"而不是"Super".
import std.stdio;
class Super
{
void opDispatch(string m)()
{
writeln("Super");
}
}
class Sub : Super
{
override void opDispatch(string m)()
{
writeln("Sub");
}
}
void main()
{
Super s = new Sub();
s.callingOpDispatch; // Writes "Super" instead of "Sub"
}
Run Code Online (Sandbox Code Playgroud)
我傻眼了,因为我无法通过使用抽象方法强制编译器查找方法覆盖(D不允许抽象模板化方法).
PS:有人可以创建标签opDispatch吗?(在我看来这对D会有好处吗?)