[英]How to force derived class to implement a static property or field?
This is my abstract class:这是我的摘要 class:
abstract class Enemy
{
protected static abstract float HEALTH
{
get;
}
float health;
void someMethod()
{
health = HEALTH;
}
}
This is my derived class:这是我派生的 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.编译器先生说我不能让成员 HEALTH static 以及 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.我的目标是强制每个孩子 class 都有一个 static 或可以从父 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. static
和继承不能一起使用。 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: 如果愿意,您可以在Enemy
提供基本的实现,也可以在不需要的情况下使其保持abstract
:
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
:通过在interface
中使用static abstract
成员,现在可以从 C#10 开始与问题的原始实现非常相似的东西:
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*:超出此范围的实施会因使用而有所不同,但是,如果希望以与原始问题类似的形式维护通用Enemy
class,则可以采用以下一种方法*:
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. *几乎肯定存在更好的选择,但同样,它们会根据预期用途而有所不同。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.