方法重载 [英] Methods overloading
问题描述
在我所说的入口点类型TemplateA的参数,我总是收到一个异常,因为第一个重载总是被调用。
我预计发生的是,最具体的方法(第二个重载)将会被调用,由于动态绑定。
任何想法,为什么
私有对象_obj?;
公共无效入口点(对象P)
{
myFunc的(P)
}
//第一个重载
私人无效myFunc的(对象容器)
{
抛出新NotImplementedException();
}
//第二个重载
私人无效myFunc的(TemplateA模板)
{
_obj =新ObjTypeA(模板);
}
//第三个重载
私人无效myFunc的(TemplateB模板)
{
_obj =新ObjTypeB(模板);
}
您将能够做这在C#4.0,如果你使用动态
而不是对象
。如果你想给它一个尝试下载的Visual Studio 2010 Beta版。在此之前,编译器选择究竟要调用基于参数的编译时类型哪种方法。
目前尚不清楚,从你的问题,你是否知道普通的单分派多态性,因为这将解决您的例子问题。
类TemplateBase
{
公共虚拟对象MYFUNC()
{
抛出新NotImplementedException();
}
}
类TemplateA:TemplateBase
{
公众覆盖对象MYFUNC()
{
返回新ObjTypeA(这个);
}
}
类TemplateB:TemplateBase
{
公众覆盖对象MYFUNC()
{
返回新ObjTypeB(这个);
}
}
和其他地方的:
私有对象_obj;
公共无效入口点(TemplateBase P)
{
_obj = p.MyFunc();
}
有,因为当你不能修改 TemplateN
类。最简单的将是入口点
方法有从访问
的一些代表。 词典
映射类型
词典<类型,函数功能与LT;对象,对象>> _myFuncs;
_myFuncs.Add(typeof运算(TemplateA),O =>新建ObjTypeA((TemplateA)O));
_myFuncs.Add(typeof运算(TemplateB),O =>新建ObjTypeB((TemplateA)O));
然后,它可以查找委托传递给它的对象类型执行。
Func键<对象,对象> F = _myFuncs [p.GetType()];
_obj = F(ρ);
但你需要,如果你想模仿的确切方式虚函数的工作来照顾了继承层次结构。
When I call the EntryPoint with a parameter of type TemplateA, I always receive an exception, since the first overload is always called.
What I expected to happen is that the most specific method (second overload) will be called due to dynamic binding.
Any ideas why?
private object _obj;
public void EntryPoint(object p)
{
myFunc(p);
}
//first overload
private void myFunc(object container)
{
throw new NotImplementedException();
}
//second overload
private void myFunc(TemplateA template)
{
_obj = new ObjTypeA(template);
}
//third overload
private void myFunc(TemplateB template)
{
_obj = new ObjTypeB(template);
}
You will be able to do this in C# 4.0 if you use dynamic
instead of object
. Download the Visual Studio 2010 Beta if you want to give it a try. Until then, the compiler chooses exactly which method to call based on the parameter's compile-time types.
It's not clear from your question whether you know about ordinary single-dispatch polymorphism, as it would solve your example problem.
class TemplateBase
{
public virtual object MyFunc()
{
throw new NotImplementedException();
}
}
class TemplateA : TemplateBase
{
public override object MyFunc()
{
return new ObjTypeA(this);
}
}
class TemplateB : TemplateBase
{
public override object MyFunc()
{
return new ObjTypeB(this);
}
}
And elsewhere:
private object _obj;
public void EntryPoint(TemplateBase p)
{
_obj = p.MyFunc();
}
There are alternatives for when you can't modify the TemplateN
classes. The simplest would be for the EntryPoint
method to have access to a Dictionary
mapping from Type
to some delegate.
Dictionary<Type, Func<object, object>> _myFuncs;
_myFuncs.Add(typeof(TemplateA), o => new ObjTypeA((TemplateA)o));
_myFuncs.Add(typeof(TemplateB), o => new ObjTypeB((TemplateA)o));
It could then look up the delegate to execute for the type of object passed to it.
Func<object, object> f = _myFuncs[p.GetType()];
_obj = f(p);
But you need to take care over inheritance hierarchies if you want to mimic the exact way virtual functions work.
这篇关于方法重载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!