C# 子类的自定义静态变量

C# 子类的自定义静态变量,c#,inheritance,static,C#,Inheritance,Static,我有这样的想法: class powerup { public static int cooldown = 1; } class freeze : powerup { //some unrelated code } class burn : powerup { //unrelated } 我想对冻结和燃烧加电的冷却时间有一个不同的值,并且是静态的,因为我不能在设置冷却时间的地方实例化它们,而且因为它们是唯一的,所以将它们设置为静态更有意义。所以我觉得我需要用“新”来覆盖冷却时间,但感觉不

我有这样的想法:

class powerup {
public static int cooldown = 1;
}

class freeze : powerup {

//some unrelated code
}
class burn : powerup {

//unrelated
}
我想对冻结和燃烧加电的冷却时间有一个不同的值,并且是静态的,因为我不能在设置冷却时间的地方实例化它们,而且因为它们是唯一的,所以将它们设置为静态更有意义。所以我觉得我需要用“新”来覆盖冷却时间,但感觉不对。有什么我不知道的解决办法吗?
提前感谢

我相信您希望更新特定通电的所有实例的冷却值。在这种情况下,我将使用如下内容:

interface IPowerup {
    int Cooldown { get; set; }
}

class Freeze : IPowerup {
    private static int _cooldown;
    public int Cooldown { get { return _cooldown } set { _cooldown = value; }
    public Freeze() { Cooldown = 1; }
}

class Burn : IPowerup {
    private static int _cooldown;
    public int Cooldown { get { return _cooldown } set { _cooldown = value; }
    public Burn() { Cooldown = 2; }
}
var burnPowerup = new Burn { Cooldown = 2 };
现在,如果你为一次加电设置冷却时间,那么你为所有加电设置冷却时间

您还可以取消构造函数,实例化通电,并按如下方式设置冷却时间:

interface IPowerup {
    int Cooldown { get; set; }
}

class Freeze : IPowerup {
    private static int _cooldown;
    public int Cooldown { get { return _cooldown } set { _cooldown = value; }
    public Freeze() { Cooldown = 1; }
}

class Burn : IPowerup {
    private static int _cooldown;
    public int Cooldown { get { return _cooldown } set { _cooldown = value; }
    public Burn() { Cooldown = 2; }
}
var burnPowerup = new Burn { Cooldown = 2 };

我相信您希望更新特定通电的所有实例的冷却时间值。在这种情况下,我将使用如下内容:

interface IPowerup {
    int Cooldown { get; set; }
}

class Freeze : IPowerup {
    private static int _cooldown;
    public int Cooldown { get { return _cooldown } set { _cooldown = value; }
    public Freeze() { Cooldown = 1; }
}

class Burn : IPowerup {
    private static int _cooldown;
    public int Cooldown { get { return _cooldown } set { _cooldown = value; }
    public Burn() { Cooldown = 2; }
}
var burnPowerup = new Burn { Cooldown = 2 };
现在,如果你为一次加电设置冷却时间,那么你为所有加电设置冷却时间

您还可以取消构造函数,实例化通电,并按如下方式设置冷却时间:

interface IPowerup {
    int Cooldown { get; set; }
}

class Freeze : IPowerup {
    private static int _cooldown;
    public int Cooldown { get { return _cooldown } set { _cooldown = value; }
    public Freeze() { Cooldown = 1; }
}

class Burn : IPowerup {
    private static int _cooldown;
    public int Cooldown { get { return _cooldown } set { _cooldown = value; }
    public Burn() { Cooldown = 2; }
}
var burnPowerup = new Burn { Cooldown = 2 };

C#中没有可重写性和静态性的组合;从某种意义上说,这些是对立的

更好的方法是制作实例;如果这有意义的话,这些实例可能是单例。我倾向于这样做:

abstract class Powerup
{
  public virtual int Cooldown => 1
}

sealed class Freeze : Powerup
{
}

sealed class Burn : Powerup
{
  public override int Cooldown => 2;
}
但当这些是单身时,我特别喜欢的一种技巧是:

abstract class Powerup
{
  private Powerup() {} // Prevent instantiation

  public virtual int Cooldown => 1

  public static readonly Powerup Freeze = new FreezePower();
  private sealed class FreezePower : Powerup
  {
  }

  public static readonly Powerup Burn = new BurnPower();
  private sealed class BurnPower : Powerup
  {
    public override int Cooldown => 2;
  }
}
现在查看使用站点:

Console.WriteLine(Powerup.Freeze.Cooldown); // 2

我认为在使用站点上这看起来真的很不错。

在C#中没有覆盖性和静态性的组合;从某种意义上说,这些是对立的

更好的方法是制作实例;如果这有意义的话,这些实例可能是单例。我倾向于这样做:

abstract class Powerup
{
  public virtual int Cooldown => 1
}

sealed class Freeze : Powerup
{
}

sealed class Burn : Powerup
{
  public override int Cooldown => 2;
}
但当这些是单身时,我特别喜欢的一种技巧是:

abstract class Powerup
{
  private Powerup() {} // Prevent instantiation

  public virtual int Cooldown => 1

  public static readonly Powerup Freeze = new FreezePower();
  private sealed class FreezePower : Powerup
  {
  }

  public static readonly Powerup Burn = new BurnPower();
  private sealed class BurnPower : Powerup
  {
    public override int Cooldown => 2;
  }
}
现在查看使用站点:

Console.WriteLine(Powerup.Freeze.Cooldown); // 2
我认为在use站点上这看起来非常不错。

您可以使用隐藏子类的父属性,例如:

class powerup
{
    public static int cooldown = 1;
}

class freeze : powerup
{
    public new static int cooldown = 3;
    //some unrelated code
}
class burn : powerup
{
    public new static int cooldown = 2;
    //unrelated
}
这提供了以下结果:

Console.WriteLine($"powerup: {powerup.cooldown}");
Console.WriteLine($"freeze: {freeze.cooldown}");
Console.WriteLine($"burn: {burn.cooldown}");

您可以使用隐藏子类的父属性,例如:

class powerup
{
    public static int cooldown = 1;
}

class freeze : powerup
{
    public new static int cooldown = 3;
    //some unrelated code
}
class burn : powerup
{
    public new static int cooldown = 2;
    //unrelated
}
这提供了以下结果:

Console.WriteLine($"powerup: {powerup.cooldown}");
Console.WriteLine($"freeze: {freeze.cooldown}");
Console.WriteLine($"burn: {burn.cooldown}");


您不能实例化冻结或刻录?没有构造函数?我可以,但在我当前的代码库中没有任何意义(这些代码库是由统一和单行为构成的)。。。我真的希望保持这部分静态,因为每种通电类型只有一个实例。如果它是用IOC创建的,那么仍然会调用默认构造函数。您可以在其中添加初始化。您不能实例化冻结或刻录吗?没有构造函数?我可以,但在我当前的代码库中没有任何意义(这些代码库是由统一和单行为构成的)。。。我真的希望保持这部分静态,因为每种通电类型只有一个实例。如果它是用IOC创建的,那么仍然会调用默认构造函数。你可以在里面添加初始化。OP提到它们不能实例化,但我相信他的意思是它们不能在设置冷却时间的地方实例化。必须看更多他的代码才能明白他的意思。现在我正在仔细观察,我相信他想要更新所有选定类型的加电冷却时间。将修改我的答案。我真的很喜欢这种方法!我将以这种方式实现它,并让您不断更新!OP提到它们不能实例化Though,我相信他的意思是它们不能在设定冷却时间的地方实例化。必须看更多他的代码才能明白他的意思。现在我正在仔细观察,我相信他想要更新所有选定类型的加电冷却时间。将修改我的答案。我真的很喜欢这种方法!我将以这种方式实现它,并让您不断更新!如果您不想使用接口,这是一种方法。简单而中肯。是的,在我的例子中,也许这是最好的方法,但我发现它很容易出错,需要为每个子类添加一个新的。无论如何谢谢你!也许我需要用这种方法做一个预测;您认为静态void M()中的T:powerup{Console.WriteLine(T.cooldown);}。。。M()是什么?现在试试看。你现在明白为什么这是一个坏主意了吗?如果你不想使用接口,这是一个好办法。简单而中肯。是的,在我的例子中,也许这是最好的方法,但我发现它很容易出错,需要为每个子类添加一个新的。无论如何谢谢你!也许我需要用这种方法做一个预测;您认为静态void M()中的T:powerup{Console.WriteLine(T.cooldown);}。。。M()是什么?现在试试看。你现在明白为什么这是个坏主意了吗?