[英]Derived types with Method overloading
代码很简单,我希望可以理解。
我正在尝试使用接口类型IColor
以便将颜色对象传递给ColorManager
。 然后,我希望ColorManager
IColor
对象作为其自己的类型传递给IColor
对象,因此将调用方法重载。
但是,似乎由于它以IColor
类型传递,因此C#不会将其隐式转换为BlueColor
或GreenColor
完整类型。
我希望这对我想要实现的目标有意义。 这在C#中可能吗?
[解决方案] http://msdn.microsoft.com/en-us/library/dd264736.aspx具有动态类型参数的重载解析
到目前为止,我的代码:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
namespace Example
{
public interface IColor
{
void CatchColor(IColor c);
}
public class BlueColor : IColor
{
public void CatchColor(IColor c)
{
}
}
public class GreenColor : IColor
{
public void CatchColor(BlueColor c)
{
Console.WriteLine("CAUGHT BLUE!");
}
public void CatchColor(GreenColor c)
{
Console.WriteLine("CAUGHT GREEN!");
}
public void CatchColor(IColor c)
{
Console.WriteLine("CAUGHT SOME COLOR!");
}
}
public class ColorManager
{
public void PassColor(IColor c)
{
// Don't use static type-checking
// Problem solved
dynamic AnyColor = c;
AnyColor.CatchColor(AnyColor);
}
public static void Main()
{
GreenColor G = new GreenColor();
new ColorManager().PassColor(G);
Console.ReadLine();
return;
}
}
}
告诉 ColorManager
类使用正确类型的传递对象的一种CatchColor
是使用一个已经实现CatchColor
的抽象类:
public abstract class IColor
{
// override in every class
public abstract void PrintColor();
// has the correct type passed with the interface
public void CatchColor(IColor c)
{
c.PrintColor();
}
}
然后,子类仅需要实现具有正确颜色的PrintColor
:
public class BlueColor : IColor
{
public override void PrintColor()
{
Console.WriteLine("BLUE!");
}
}
public class GreenColor : IColor
{
public override void PrintColor()
{
Console.WriteLine("GREEN!");
}
}
经理是一样的:
public class ColorManager
{
public void PassColor(IColor c)
{
c.CatchColor(c);
}
}
可以这样使用:
GreenColor G = new GreenColor();
var cm = new ColorManager();
cm.PassColor(G);
cm.PassColor(new BlueColor());
输出为:
GREEN!
BLUE!
您想要的是后期方法绑定。
不利的一面是您必须为每种新型颜色添加方法。 好处是您不必维护案例陈述或条件逻辑。
有关更多详细信息,请参见此处: 早期和晚期绑定
编辑:这是这种后期绑定的有效示例。
class Program {
static void Main(string[] args) {
//Declare instances
BaseClass myClass = new Class2();
BaseClass otherClass = new Class1();
//Invoke the action method which will match based on the BaseClass type
Action(myClass);
Action(otherClass);
Console.ReadLine();
}
public static void Action(BaseClass classType) {
//Remove the compile-time type so the runtime can select the method based on signature
dynamic aClass = classType;
ServiceMethod(aClass);
}
public static void ServiceMethod(dynamic input) {
Methods(input);
}
public static void Methods(Class1 classType) {
Console.WriteLine("Class1");
Debug.WriteLine("Class1");
}
public static void Methods(Class2 classtype) {
Console.WriteLine("Class2");
Debug.WriteLine("Class2");
}
public static void Methods(Class3 classType) {
Console.WriteLine("Class3");
Debug.WriteLine("Class3");
}
}
public abstract class BaseClass { //This could also be an interface
public Guid Id { get; set; }
public string Name { get; set; }
}
public class Class1 : BaseClass {
}
public class Class2 : BaseClass{
}
public class Class3 : BaseClass {
}
所以你想要像这样的东西:
public void CatchColor(Color c)
{
if (c is BlueColor)
CatchColor(c as BlueColor);
if (c is GreenColor)
CatchColor(c as GreenColor);
}
?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.