Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.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# Fighter类不实现接口成员_C#_Class_Interface - Fatal编程技术网

C# Fighter类不实现接口成员

C# Fighter类不实现接口成员,c#,class,interface,C#,Class,Interface,我得到了这个错误: 错误1“Fight.Fighter”未实现接口成员 “战斗,我战斗,我战斗” 这是我第一次尝试学习如何使用接口,很抱歉提出这个问题 有什么想法吗 我有以下代码: 接口: interface IFighter { string GraphicLife(); bool IsLive(); int Obrana(int utocneCislo); void Utok(IFighter bojovnik)

我得到了这个错误:

错误1“Fight.Fighter”未实现接口成员 “战斗,我战斗,我战斗”

这是我第一次尝试学习如何使用接口,很抱歉提出这个问题

有什么想法吗

我有以下代码:

接口:

    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;
    }

}}
class战斗机:IFighter
{
受保护的字符串名称;
保护生命;
受保护的生命;
受保护的int攻击;
受保护的内部防御;
受保护的Kostka Kostka;
公共战斗机(字符串名称、int-life、int-maxLife、int-attack、int-defence、Kostka-Kostka){
this.name=名称;
这就是生活;
this.maxLife=maxLife;
这个攻击=攻击;
这就是防御;
this.kostka=kostka;
}
公共图书馆
{
如果(寿命>0)
{
返回true;
}
否则返回false;
}
公共字符串GraphicLife()
{
int-pozic=20;
int numberOfParts=(int)数学圆(((双)寿命/(双)最大寿命)*(双)pozic);
string zivot=string.Concat(可枚举的.Repeat(“#”,numberOfParts));
zivot=zivot+String.Concat(可枚举的.Repeat(“,”pozic-numberOfParts));
zivot=“[”+zivot+“]”;
返回zivot;
}
公共真空乌托克(战士)
{
if(warrior.IsLive())
{
int utok=(int)Math.Round((双)攻击/(双)kostka.getPocetStran()*(双)kostka.getNumber());
int obrana=战士。obrana(乌托克);
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.graphicslife());
Console.WriteLine(warrior.name+“-”+warrior.life);
Console.WriteLine(warrior.graphicslife());
}
else Console.WriteLine(this.name+“utoci na mrtvolu”);
}
公共内部Obrana(内部攻击编号)
{
int localDefence=(int)Math.Round((双)defence/(双)kostka.getPocetStran()*(双)kostka.getNumber());
int utok=攻击编号-本地防御;
如果(utok<0)utok=0;
生活=生活-乌托克;
返回本地防御;
}
}}

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

更改以下行

public void Utok(Fighter warrior)

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

如果您在创建类之前定义了接口(这是最常见的方法),那么您可以使用VisualStudio提供的一个很好的助手为您完成一些工作。将光标指向接口名称,并使用“实现接口”函数自动为接口创建方法存根

编辑:

您还需要将属性“Name”添加到接口以使其正常工作。它必须是至少需要一个getter的属性:

string name { get; }
普通变量而不是getter在这里不起作用,因为接口不能包含变量


只有接口的属性可用,而不管应用程序中其他地方实际实现该接口的类有多少。

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

更改以下行

public void Utok(Fighter warrior)

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

如果您在创建类之前定义了接口(这是最常见的方法),那么您可以使用VisualStudio提供的一个很好的助手为您完成一些工作。将光标指向接口名称,并使用“实现接口”函数自动为接口创建方法存根

编辑:

您还需要将属性“Name”添加到接口以使其正常工作。它必须是至少需要一个getter的属性:

string name { get; }
普通变量而不是getter在这里不起作用,因为接口不能包含变量


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

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

public void Utok(IFighter warrior)
{
    // ...
}
要实现接口成员,实现类的相应成员必须是公共的、非静态的,并且与接口成员具有相同的名称和签名


这意味着完全相同的签名

您对
Utok
的方法签名需要一个
IFighter
实例,而不是接口合同中定义的
Fighter

public void Utok(IFighter warrior)
{
    // ...
}
要实现接口成员,实现类的相应成员必须是公共的、非静态的,并且与接口成员具有相同的名称和签名


这意味着完全相同的签名

右键单击类定义中的接口并选择implement interface右键单击类定义中的接口并选择implementinterface@aruzkaty:这是因为
IFighter
不包含这些内容。这听起来像是利斯科夫替代问题的兔子洞。如果任何
IFighter
都应该公开这些属性,那么它们应该作为属性添加到该接口中。我明白了,我认为该接口可以只包含方法,而不包含变量。@aruzkaty:正确。虽然属性是一种方法。@aruzkaty:那是因为
IFighter
不包含这些东西。这听起来像是利斯科夫替代问题的兔子洞。如果