简体   繁体   中英

How to force derived class to implement a static property or field?

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?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM