微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为委托分配不同的工厂

如何解决为委托分配不同的工厂

我有多个工厂接受一个 int 并返回一个 Creature 类型的对象:

public static Func<int,Creature>[] ForestEnemyFactory1 = new Func<int,Creature>[] 
        {
            (level) => (Creature) new Spider(level),(level) => (Creature) new Ogre(level),};

    public static Func<int,Creature>[] ForestEnemyFactory2 = new Func<int,Creature>[]
    {
        (level) => (Creature) new ArmourBug(level),(level) => (Creature) new GlowBug(level)
    };

调用它们的函数应该是有条件的。

if (...) {
Enemy1 = ForestEnemyFactory1[x](y);
else

现在我想让函数总是调用一个委托,并决定在其他地方调用哪个。

delegate Del = ...   
Enemy1 = delegate (int positionInArray,int level)

然后在别处定义:

if (...){
   delegate = ForestEnemyFactory2; 

但无论我如何尝试,我都无法找到如何定义委托并让他接受其中一个工厂的解决方案。有人可以帮忙吗?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。