Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用多态性获取相同类型的实例_C#_Architecture_Polymorphism - Fatal编程技术网

C# 使用多态性获取相同类型的实例

C# 使用多态性获取相同类型的实例,c#,architecture,polymorphism,C#,Architecture,Polymorphism,我有下一个场景: 有一个类负责管理应用程序的所有货币。所有的货币都扩展了货币,所以它可以表现得像它一样。货币是抽象的,因此无法实例化。 在应用程序的某些部分,我使用软、硬或事件货币作为成本货币,例如,玩家按下某些菜单上的某些购买按钮。此操作触发购买,该购买以货币作为参考,在本例中,它可以是costSoftCurrency。 其思想是,如果玩家货币收到一种货币,玩家货币评估他的货币并返回相同类型的关联货币,然后您可以安全地减去成本 我的问题是。。。如果没有这些可怕的假设,我怎么能得到同样的逻辑呢?

我有下一个场景: 有一个类负责管理应用程序的所有货币。所有的货币都扩展了货币,所以它可以表现得像它一样。货币是抽象的,因此无法实例化。 在应用程序的某些部分,我使用软、硬或事件货币作为成本货币,例如,玩家按下某些菜单上的某些购买按钮。此操作触发购买,该购买以货币作为参考,在本例中,它可以是costSoftCurrency。 其思想是,如果玩家货币收到一种货币,玩家货币评估他的货币并返回相同类型的关联货币,然后您可以安全地减去成本

我的问题是。。。如果没有这些可怕的假设,我怎么能得到同样的逻辑呢?我读过,但我不知道它是否适用于这里

有人知道这能否实现吗

公共类玩家币
{
公共软货币软货币=新软货币();
公共硬通货硬通货=新硬通货();
public EventCurrency EventCurrency=新的EventCurrency();
公共货币GetCurrencyFromType(货币)
{
if(货币为软货币)
{
返回此.softCurrency;
}
if(货币为硬通货)
{
返回此。硬通货;
}
如果(货币为事件货币)
{
返回此.eventCurrency;
}
返回null;
}
}
公共抽象类货币
{
公众浮动价值
{
得到;
设置
}
公共作废添加(货币)
{
此.Value+=货币.Value;
}
公开作废减记(货币)
{
this.Value-=currency.Value;
}
}
公共类EventCurrency:货币
{
}
公共类硬通货:货币
{
}
公共类软货币:货币
{
}
内部课程计划
{
私有静态void Main(字符串[]args)
{
PlayerCurrences PlayerCurrences=新的PlayerCurrences();
货币成本软货币=新软货币();
货币成本硬货币=新硬货币();
playerCurrences.GetCurrencyFromType(costSoftCurrency).Substract(costSoftCurrency);//在这里,我从playerCurrences中获得了一个软货币
playerCurrences.GetCurrencyFromType(costHardCurrency).Substract(costHardCurrency);//在这里,我从玩家货币中获得了硬货币
}
}

我将使用factory方法设计模式,因为它特别适用于使用子类来确定应该创建哪些类的时候。这基本上就是你在这里做的。我最初的回答提到了反射,因为我在我链接到的网站上看到了映射配置,觉得很酷,但在昨晚仔细考虑之后,它也有很多开销。使用字典绕过if块或switch语句会简单得多,速度也快得多

以下是我将添加/更改的代码:

//The Factory class that creates the different types of currency based
//on a name parameter
static class CurrencyFactory
{
    private static readonly Dictionary<string, Currency> _currencyDictionary =
        new Dictionary<string, Currency>
    {
        { "HardCurrency", new HardCurrency() },
        { "SoftCurrency", new SoftCurrency() },
        { "EventCurrency", new EventCurrency() }
    };

    public static Currency Create(string currencyTypeName)
    {
        return _currencyDictionary[currencyTypeName];
    }
}

internal class Program
{
    private static void Main(string[] args)
    {
        //This class will no longer be needed as it is redundant
        PlayerCurrencies playerCurrencies = new PlayerCurrencies();

        var costSoftCurrency = CurrencyFactory.Create("softCurrency");
        var costHardCurrency = CurrencyFactory.Create("hardCurrency");

        //I wasn't sure of your goals here so I kept it as is,
        //but as I said above, playerCurrencies won't be needed.
        playerCurrencies.GetCurrencyFromType(costSoftCurrency)
            .Substract(costSoftCurrency); // there i get a SoftCurrency from Player Currencies 

        playerCurrencies.GetCurrencyFromType(costHardCurrency)
            .Substract(costHardCurrency); // there i get a HardCurrency from Player Currencies 
    }
}
//创建基于货币的不同类型的工厂类
//关于名称参数
静态类工厂
{
专用静态只读字典\u currencyDictionary=
新词典
{
{“硬通货”,新的硬通货(),
{“软货币”,新的软货币(),
{“EventCurrency”,新的EventCurrency()}
};
公共静态货币创建(字符串currencyTypeName)
{
return_currencyDictionary[currencyTypeName];
}
}
内部课程计划
{
私有静态void Main(字符串[]args)
{
//这个类不再需要,因为它是多余的
PlayerCurrences PlayerCurrences=新的PlayerCurrences();
var costSoftCurrency=CurrencyFactory.Create(“软货币”);
var costHardCurrency=CurrencyFactory.Create(“硬货币”);
//我不确定你在这里的目标,所以我保持原样,
//但正如我上面所说,不需要PlayerCurrency。
PlayerCurrences.GetCurrencyFromType(costSoftCurrency)
.Substract(costSoftCurrency);//在这里,我从玩家货币中获得了一个软货币
PlayerCurrences.GetCurrencyFromType(costHardCurrency)
.Substract(costHardCurrency);//在这里,我从玩家货币中得到了一个硬货币
}
}
您可以在这里找到一个更全面的示例和对该模式的很好的解释:

您可以使用以下工具来完成此操作:

我将字段的名称更改为
事件
,以便更容易地将它们与具有相同名称的类型区分开来

在这个特定的示例中,传入
currency
参数并仅对其类型使用它似乎有些奇怪。也许有另一种方法可以解决您的特定问题。但是关于如何在没有多个
if
语句的情况下实现这一点,您可以这样做

您经常会看到这一点在声明变量时得到了演示,如下所示:

switch (currency)
{
    case EventCurrency ec : return Event;
    case HardCurrency hc: return Hard;
    case SoftCurrency sc: return Soft;
}
return null;

例如,如果
currency
的类型为
EventCurrency
,则在
案例
的范围内,
ec
将是
currency
参数转换为
EventCurrency
。但在本例中,由于您没有使用值,因此不需要变量。

听起来访客设计模式可以帮助您维护每种不同货币的集合。然后,要按类型检索货币,您可以使用
.OfType()
Tbh为不同的货币创建所有不同的类,然后混用类型似乎是过度工程化了。就功能而言,货币之间没有区别,所以对我来说,它只应该有一个类别。创建一个
enum
以区分不同的货币类型,并将其传递到其构造函数中(使其成为只读字段),并在中保留一个
字典
switch (currency)
{
    case EventCurrency ec : return Event;
    case HardCurrency hc: return Hard;
    case SoftCurrency sc: return Soft;
}
return null;