使用方法重载的派生类型

时间:2014-06-04 16:45:09

标签: c#

代码很简单,我希望能理解。

我尝试使用接口类型IColor将颜色对象传递给ColorManager。然后我希望ColorManager将此对象作为自己的类型传递给IColor对象,因此调用方法重载。

但是,它似乎是以IColor类型传递的,因此C#不会将其作为BlueColorGreenColor强制转换为完整类型。

我希望这对我想达到的目标有所帮助。这在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;
        }
    }
}

3 个答案:

答案 0 :(得分:2)

告诉 ColorManager类使用正确类型的传递对象的一种可能性是使用一个已经实现{{ 1}}:

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());

答案 1 :(得分:1)

你想要的是晚期方法绑定。

这样做的缺点是你必须为每种新颜色添加方法。好处是你不必维护案例陈述或条件逻辑。

请点击此处了解更多详情: Early and late binding

编辑:以下是此类后期绑定的一个有效示例。

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 {
}

答案 2 :(得分:0)

所以你想要这样的东西:

    public void CatchColor(Color c)
    {
        if (c is BlueColor)
            CatchColor(c as BlueColor);
        if (c is GreenColor)
            CatchColor(c as GreenColor);
    }