如何访问接口类型为c#的数组成员

时间:2016-08-19 03:05:52

标签: c#

我在这里有点迷失。

基本上,我需要访问一个数组项,一个字符串并显示它。这是代码。

namespace Test3_2_Practice
{
public partial class InterfaceImplementation : Form
{
    //Array
    ICombatant[] combatants = new ICombatant[2];

    public InterfaceImplementation()
    {
        InitializeComponent();
    }

    private void btnTest_Click(object sender, EventArgs e)
    {
        combatants[0] = new PlayerCharacter ("Conan" , 500);
        combatants[1] = new MonsterCharacter ("Bob" , 5);
        combatants[2] = new MonsterCharacter ("Snake" , 15);

        string output = "Fighters" + Environment.NewLine;

        for (var i = 0; i < combatants.Length; i++)
        {
            var character = combatants[i];
            output += "Character:" + combatants[i].
        }
    }
}

}

所以我有我的阵列,战斗员由两种类型的实例组成。我想访问这个名字,&#34;柯南&#34;并将其添加到字符串以进行输出。我该怎么做呢?如果有帮助,这是代码的其余部分。谢谢!

namespace Test3_2_Practice
{
//Interface
interface ICombatant
{
    int TakeDamage(int damageAmount);
    string GetHealthDisplay();
}

class PlayerCharacter : ICombatant
{
    private string characterName;
    private int currentHealth;
    private int maxHealth;

    public string CharacterName
    {
        get { return characterName; }
        set { characterName = value; }
    }
    public int CurrentHealth
    {
        get { return currentHealth; }
        set { currentHealth = value; }
    }
    public int MaxHealth
    {
        get { return maxHealth; }
        set { maxHealth = value; }
    }

    public PlayerCharacter(string characterName, int maxHealth)
    {
        CharacterName = characterName;
        CurrentHealth = MaxHealth = maxHealth;
    }

    //Damage Class
    public int TakeDamage(int damageAmount)
    {
        if (damageAmount > currentHealth)
        {
            damageAmount = currentHealth;
            return damageAmount;
        }
        else
        {
            currentHealth = currentHealth - damageAmount;
            return damageAmount;
        }
    }

    //Health Class
    public string GetHealthDisplay()
    {
        return ("Health " + CurrentHealth.ToString() + "/" + MaxHealth).ToString();
    }
}

class MonsterCharacter : ICombatant
{
    private string monsterName;
    private int health;

    public string MonsterName
    {
        get { return monsterName; }
        set { monsterName = value; }
    }
    public int Health
    {
        get { return health; }
        set { health = value; }
    }

    public MonsterCharacter(string monsterName, int health)
    {
        MonsterName = monsterName;
        Health = health;
    }

    //Damage Class
    public int TakeDamage(int damageAmount)
    {
        if(damageAmount > health)
        {
            damageAmount = health;
            return damageAmount;
        }
        else
        {
            health = health - damageAmount;
            return damageAmount;
        }
    }

    //Health Class
    public string GetHealthDisplay()
    {
        return "Health " + Health;
    }
}
}

2 个答案:

答案 0 :(得分:0)

实际上,由于name对于所有接口的实现者都是通用的,Name属性应该包含在接口中,如

interface ICombatant
{
    int TakeDamage(int damageAmount);
    string GetHealthDisplay();
    public string CharacterName
    {
        get;
        set;
    }
}

在您当前的场景中,您必须在访问之前将其转换为特定的具体类型

var character = combatants[i];
if(character is PlayerCharacter)
   output += "Character:" + ((PlayerCharacter)character).CharacterName;

答案 1 :(得分:0)

最好充分利用您的界面。在界面中创建一个新方法:

interface ICombatant
{
    int TakeDamage(int damageAmount);
    string GetHealthDisplay();
    string GetCombatantName(); // added this
}

然后在两个实现它的类中实现:

class PlayerCharacter : ICombatant
{

    // ... a lot of code ...

    public string GetCombatantName()
    {
        return String.Format("Character: {0}", this.CharacterName);
    }
}

class MonsterCharacter: ICombatant
{

    // ... a lot of code ...

    public string GetCombatantName()
    {
        return String.Format("Monster: {0}", this.MonsterName);
    }

}

并像这样使用它:

private void btnTest_Click(object sender, EventArgs e)
{
    combatants[0] = new PlayerCharacter("Conan", 500);
    combatants[1] = new MonsterCharacter("Bob", 5);
    combatants[2] = new MonsterCharacter("Snake", 15);

    string output = "Fighters" + Environment.NewLine;

    foreach (var combatant in combatants)
    {
        output += combatant.GetCombatantName();
    }
}

因此,如果有一天你得到十种不同类型的ICombatant(如AnimalCharacter,VirusCharacter),你就不必嵌套很多ifs来检查和转换类型以获得适当的财产。

接口完全是为了避免这种东西,隐藏实现细节。