【问题标题】:Fighter class does not implement interface memberFighter类没有实现接口成员
【发布时间】:2015-06-17 13:37:37
【问题描述】:

我收到了这个错误:

错误 1 ​​'Fight.Fighter' 没有实现接口成员 'Fight.IFighter.Utok(Fight.IFighter)'

这是我第一次尝试学习使用接口,非常抱歉转储问题。

有什么想法吗?

我有以下代码:

界面:

    interface IFighter
    {
        string GraphicLife();
        bool IsLive();
        int Obrana(int utocneCislo);
        void Utok(IFighter bojovnik);
    }
}

类:

class Fighter : IFighter
{
    protected string name;
    protected int life;
    protected int maxLife;
    protected int attack;
    protected int defence;

    protected Kostka kostka;

    public Fighter(string name, int life, int maxLife, int attack, int defence, Kostka kostka){
        this.name = name;
        this.life = life;
        this.maxLife = maxLife;
        this.attack = attack;
        this.defence = defence;
        this.kostka = kostka;
    }

    public bool IsLive()
    {
        if (life > 0)
        {
            return true;
        }
        else return false;
    }

    public string GraphicLife()
    {
        int pozic = 20;
        int numberOfParts = (int)Math.Round(((double)life / (double)maxLife) * (double)pozic);
        string zivot = String.Concat(Enumerable.Repeat("#", numberOfParts));
        zivot = zivot + String.Concat(Enumerable.Repeat("_", pozic - numberOfParts));
        zivot = "[" + zivot + "]";
        return zivot;
    }

    public void Utok(Fighter warrior)
    {
        if (warrior.IsLive())
        {
            int utok = (int)Math.Round((double)attack / (double)kostka.getPocetStran() * (double)kostka.getNumber());
            int obrana = warrior.Obrana(utok);
            Console.WriteLine(this.name + "utoci na " + warrior.name + " silou " + utok + " " + warrior.name + " se brani silou " + obrana);
            Console.WriteLine(this.name + " - " + this.life);
            Console.WriteLine(this.GraphicLife());
            Console.WriteLine(warrior.name + " - " + warrior.life);
            Console.WriteLine(warrior.GraphicLife());
        }
        else Console.WriteLine(this.name + " utoci na mrtvolu");
    }

    public int Obrana(int attackNumber)
    {
        int localDefence = (int)Math.Round((double)defence/ (double)kostka.getPocetStran() * (double)kostka.getNumber());
        int utok = attackNumber - localDefence;
        if (utok < 0) utok = 0;
        life = life - utok;
        return localDefence;
    }

}}

【问题讨论】:

  • 类定义中的接口右键,选择实现接口

标签: c# class interface


【解决方案1】:

您在方法的参数列表中使用具体类型 Fighter 而不是抽象类型 IFighter

更改以下行

public void Utok(Fighter warrior)

public void Utok(IFighter warrior)

如果在类中实现,则需要使用接口中定义的确切类型

如果您在创建类之前定义接口(这是最常见的方式),您可以使用 Visual Studio 提供的好帮手来为您完成一些工作。将光标指向接口名称并使用“实现接口”功能自动为您的接口创建方法存根。

编辑:

您还需要将属性“名称”添加到界面以使其工作。它必须是至少需要一个 getter 的属性:

string name { get; }

普通变量而不是 getter 在这里不起作用,因为接口不能包含变量。

无论有多少类在应用程序的其他地方实际实现了该接口,只有接口的属性可用。

【讨论】:

    【解决方案2】:

    Utok 的方法签名需要 IFighter 的实例,而不是接口合约定义的 Fighter

    public void Utok(IFighter warrior)
    {
        // ...
    }
    

    要实现接口成员,实现类的对应成员必须是公共的、非静态的,并且与接口成员具有相同的名称和签名

    这意味着完全相同的签名

    【讨论】:

    • @aruzkaty:那是因为IFighter 不包含这些东西。这听起来像是 Liskov Substitution 问题的兔子洞。如果这些是任何IFighter 应该公开的属性,那么它们应该作为属性添加到该接口。
    • 我明白了,我认为接口可以只包含方法,而不是变量。
    • @aruzkaty:正确。虽然属性是一种方法。
    猜你喜欢
    • 1970-01-01
    • 2016-05-08
    • 2015-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多