方法重载和多态性
class Program
{
static void Main(string[] args)
{
List<A> myList = new List<A> {new A(), new B(), new C()};
foreach (var a in myList)
{
Render(a);
}
Console.ReadKey();
}
private static void Render(A o)
{
Console.Write("A");
}
private static void Render(B b)
{
Console.Write("B");
}
private static void Render(C c)
{
Console.Write("C");
}
}
class A
{
}
class B : A
{
}
class C : A
{
}
输出为: AAA
是否可以以某种方式使用方法重载,以便输出为: ABC?
class Program
{
static void Main(string[] args)
{
List<A> myList = new List<A> {new A(), new B(), new C()};
foreach (var a in myList)
{
Render(a);
}
Console.ReadKey();
}
private static void Render(A o)
{
Console.Write("A");
}
private static void Render(B b)
{
Console.Write("B");
}
private static void Render(C c)
{
Console.Write("C");
}
}
class A
{
}
class B : A
{
}
class C : A
{
}
The output is: AAA
Is it possible to somehow use method overloading, so that the output would be: ABC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您使用的是 C# 4,则可以使用动态类型:
在静态类型中,重载决策在编译时执行,而不是在执行时执行。
对于在决策时选择的实现,您要么必须使用覆盖而不是重载,要么使用上面的动态类型。
You can use dynamic typing if you're using C# 4:
Within static typing, overload resolution is performed at compile-time, not at execution time.
For the implementation to be chosen at decision time, you either have to use overriding instead of overloading, or use dynamic typing as above.
以下应该可以解决问题,我们可以在使用该类型中的类型时控制行为:
如果您希望类型的定义行为添加到其父级的行为中,则在之后调用基类中实现的方法执行你自己的逻辑,例如:
The following ought to do the trick, where we control the behaviour when working with a type within that type:
And if you want the defined behaviour of a type to be additive to that of its parent, then call the method implemented in the base after executing your own logic, for example:
实现此目的的另一种方法是使用访问者模式:它允许您实现类似的目标 使用双向方法调用系统的多态性:
这种方法的优点是可以有效地实现多态性(每种类型通过传递强类型的
this
内部渲染),同时保留不属于您的类型本身的逻辑(例如,视觉渲染逻辑)。Another way to accomplish this is with the visitor pattern: it allows you to achieve something like polymorphism using a two-way method calling system:
The advantage to this approach is that it allows you to effectively achieve polymorphism (each type ensures the correct overload is called by passing the strongly-typed
this
toRender
internally) while keeping logic that does not belong in your types themselves (e.g., visual rendering logic) out.使 ABC 派生自基(抽象)类,在该类中定义方法 Render 并在每个 ABC 中正确重写。不要先调用
Render(a)
,然后再调用a.Render()
,这才是多态性的工作方式。Make A B C deriving from a base ( abstract ) class, define in that class a method Render and override properly in each A B C . Instead of calling
Render(a)
then calla.Render()
this is the way polymorfism is supposed to work.