1

This is my abstract class:

abstract class Enemy
{
    protected static abstract float HEALTH
    {
        get;
    }

    float health;

    void someMethod()
    {
        health = HEALTH;
    }
}

This is my derived class:

abstract class BadGuy : Enemy
{
    protected override static float HEALTH
    {
        get { return 1; }
    }
}

Mr. Compiler says I can't make the member HEALTH static as well as abstract in the Enemy class.

My goal is to force each child class to have a static or constant field which can be accessed from the parent class.

Is there a solution for this? If not, what's the most elegant workaround? Making the property non-static?

H.B.
  • 166,899
  • 29
  • 327
  • 400
xoxox
  • 719
  • 1
  • 13
  • 24

2 Answers2

6

static and inheritance don't work together. What you can do is make a virtual property which can be overridden in the derived class. If you wish, you can either provide a base implementation inside Enemy, or keep it abstract if you don't want to:

public abstract class Enemy
{
    protected abstract float Health { get; }
}

public class BadGuy : Enemy
{
    private const int BadGuyHealth = 1;
    protected override float Health
    {
        get { return BadGuyHealth; }
    }
}

public class EvenWorseGuy : BadGuy
{
    private const int WorseGuyHealth = 2;
    protected override float Health
    {
        get { return WorseGuyHealth; }
    }
}
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • 1
    Thank you. Why is static together with inheritance seen as problematic or a discrepancy? That does'nt make sense to me. – xoxox Jan 10 '16 at 13:05
  • @Myself http://stackoverflow.com/questions/774181/why-cant-i-inherit-static-classes – xoxox Jan 15 '16 at 16:10
0

Something very similar to the question's original implementation is now possible starting with C#10, by using static abstract members in an interface:

public interface IEnemy
{
    public static abstract float Health { get; }

    public float GetHealth()
    {
        return (float)GetType().GetProperty(nameof(Health)).GetValue(null);
    }
}

public class BadGuy : IEnemy
{
    public static float Health => 1f;
}

Implementation beyond this will vary somewhat depending on use, however if one desires to maintain a generic Enemy class in a similar form to that of the original question then one of the ways this could be done is as follows*:

public class Enemy
{
    private float health;

    public void SetEnemy(IEnemy enemy)
    {
        health = enemy.GetHealth();
    }
}

*Better options almost certainly exist, but again, they will vary based on intended use.

Pikanchion
  • 365
  • 3
  • 8