【发布时间】: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;
}
}}
【问题讨论】:
-
类定义中的接口右键,选择实现接口