抽象类与抽象模糊原型的方法在C#中不起作用

时间:2013-12-25 08:30:46

标签: c# interface abstract-class method-overriding

假设我有2个类Player和Npc以及一个抽象类Entity。

如何让Entity类忽略原型中的模糊类,它不应该关注它的抽象 我看到的唯一选择就是为两者选择一个Wrapper,这似乎是一件非常愚蠢的事情。

Npc class

class Npc : Entity {
    private NpcSkills skills; //npc skills
    ...

    public Npc(int id) 
    {
        this.id = id;
        skills = new NpcSkills(this); //<- Yes its a new class NpcSkills not Skills.
        ...
    }

    public override NpcSkills getSkills() //<-- Method has same name, different class then Player.
    {
        return skills;
    }
    ...
}

玩家类

class Player : Entity
{
    private Skills skills;
    ...

    public Player(int id) 
    {
        this.id = id;
        skills = new Skills(this); //<- Yes its a new class NpcSkills not Skills.
        ...
    }

    public override Skills getSkills() //<-- Method has same name, different class then Npc.
    {
        return skills;
    }
    ...
 }

实体类

abstract class Entity
{
    private int id;

        public abstract void getSkills(); //<-- fail how would I match this against 2 classes
        //public abstract Skills getSkills(); //<-- fail for npc
        //public abstract NpcSkills getSkills(); //<-- fail for player
        //public abstract SkillsWrapper getSkills(); //<-- won't take this path haha.

        public void updateDamageTaken(double damageTaken)
        {
            int currentHp = getSkills().getHealth(); //<-- want it like this.
        }
}

1 个答案:

答案 0 :(得分:3)

您可以从同一个基类派生SkillsNpcSkills类,比如SkillsBase,并按如下方式声明您的getSkills方法:

public abstract SkillsBase getSkills();