简体   繁体   English

C# 我可以将接口方法添加到列表中吗?

[英]C# Can I add Interface methods to a list?

I just started programming and I dont even know if this is even possible...我刚开始编程,我什至不知道这是否可能......

I have the following Interface:我有以下界面:

public interface ISkills
{
    int SelfHeal();
    int AditionDamage();
    int DoubleDamage();
    int DefenseMatrix();
}

and the following base class:和以下基地 class:

class NetNavi : ISkills
{
    List<ISkills> skills = new List<ISkills>();
     
    int ISkills.SelfHeal()
    {
        return 5;
    }

    int ISkills.AditionDamage()
    {
        return 5;
    }

    int ISkills.DoubleDamage()
    {
        return 5;
    }

    int ISkills.DefenseMatrix()
    {
        return 5;
    }
     
    
    public void AddSkills(int chosenSkill)
    {
        switch(chosenSkill)
        {
            case 1:
                //add SelfHeal to the list
                break;
            case 2:
                //add AditionDamage to the list
                break;
            case 3:
                //add DoubleDamage to the list
                break;
            case 4:
                //add DefenseMatrix to the list
                break;
        }
    }
}

In main the user gets asked to choose 2 skills.主要是要求用户选择 2 项技能。 How can I add the chosen methods to my list (because the add() doesnt work...) and how can I call the methods in the list then?我怎样才能将选择的方法添加到我的列表中(因为 add() 不起作用......)然后我怎样才能调用列表中的方法?

Here:这里:

class NetNavi : ISkills
    {
        Dictionary<string, Func<int>> skills = new Dictionary<string, Func<int>>();
     
        int SelfHeal()
        {
            return 5;
        }

        int AditionDamage()
        {
            return 5;
        }

        int DoubleDamage()
        {
            return 5;
        }

        int DefenseMatrix()
        {
            return 5;
        }
     
    
            
       

 public void AddSkills(int chosenSkill)
        {
            switch(chosenSkill)
            {
                case 1:
                    Func<int> theFunc = SelfHeal;
                    skills.Add("SelfHeal", theFunc);
                    break;
                case 2:
                    Func<int> theFunc = AditionDamage;
                    skills.Add("AditionDamage", theFunc);
                    break;
                case 3:
                    Func<int> theFunc = DoubleDamage;
                    skills.Add("DoubleDamage", theFunc);
                    break;
                case 4:
                    Func<int> theFunc = DefenseMatrix;
                    skills.Add("DefenseMatrix", theFunc );
                    break;
            }   
        }
}

In which by typing skills["DefenseMatrix"]() (or instead of "DefenseMatrix" any other key "key" you defined in skills.Add("key", theFunc); ) you execute the method.在其中通过键入skills["DefenseMatrix"]() (或者代替"DefenseMatrix" ,您在skills.Add("key", theFunc);中定义的任何其他键"key" )执行该方法。

If there is no matching "key" found this will raise an error.如果没有找到匹配的"key" ,这将引发错误。

You can check if a key exists by if (skills.ContainsKey("key"))您可以通过if (skills.ContainsKey("key"))检查密钥是否存在

If you actually just wanted to determine which methods the character is allowed to do and which not, you might benefit from looking into [Flags] enum 's.如果您实际上只是想确定角色可以使用哪些方法,哪些方法不能使用,那么查看[Flags] enum可能会有所帮助。 It seems [Flags] would be a better way.看来[Flags]会是更好的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM