在 C# 中,派生值可以同时被静态和实例 methods/properties 引用吗?

in C#, can a derived value be referred to by both static and instance methods/properties?

我是 C# 的新手,我一直在用头撞墙试图弄清楚如何实现与实例和静态属性相关的特定设计模式,从派生 class。似乎有一些限制可以防止这种情况发生,所以我想解释一下我的问题,看看是否有人对如何解决这个问题有想法。

public class Resource {
    protected static float s_unitMass = 1.0f;
    protected static string s_name = "<Resource>";
    public int quantity;

    // Class Convenience
    static public float GetUnitMass() {
        return s_unitMass;
    }
    static public string GetName() {
        return s_name;
    }

    // Instance Convenience
    virtual public float totalMass {
        get { return s_unitMass * quantity; }
    }
    virtual public float unitMass {
        get { return s_unitMass; }
    }
    virtual public string name {
        get { return s_name; }
    }
}

public class Iron : Resource {
    new protected static s_unitMass = 2.0f;
    new protected static s_name = "Iron";
}

此代码非常无效(基本 class 资源的值始终返回),但我以这种方式写出来以表明我 喜欢什么 to do... 有一个可以被两者引用的值:

string name = Iron.GetName();
float unitMass = Iron.GetUnitMass();

Iron iron = new Iron();
string name = iron.name;
float unitMass = iron.unitMass;
float totalMass = iron.totalMass;

如果真的想要这样,那么

// Have a [static] singleton Iron instance, but doesn't prevent
// creation of new Iron instances..
static class ResourceTable {
    public static Iron Iron = new Iron();
};

// Just an Iron instance; it doesn't matter where it comes from
// (Because it is a 'singleton' the SAME instance is returned each time.)
Iron iron = ResourceTable.Iron;    // or new Iron();
                                   //    [^- object ]

// .. and it can be used the same
string name = iron.name;           // or ResourceTable.Iron.name, eg.
float unitMass = iron.unitMass;    //    [^- object too!  ]
float totalMass = iron.totalMass;

现在,做一些笔记。

  1. 一般单例不允许"alternative methods of creation";可变单例是.. icky;并且,

  2. 这是类型的过度专业化(例如IronFeather、..);并且,

  3. 元素类型(与特定质量相关,例如)可能应该与数量 分开,因为可能有多个数量与贯穿整个问题的资源。

考虑:

static Resource {
    public string Name { get; set; }
    public float UnitMass { get; set; }
}

static Bucket {
    public Resource FilledWith { get; set; }
    public int Quantity { get; set; }
    public float TotalMass {
      get { return FilledWith.UnitMass * Quantity; }
    }
}

static class ResourceTable {
    public Resource Iron =
      new Resource { Name = "Iron", UnitMass = 1 };
    public Resource Feather =
      new Resource { Name = "Feather", UnitMass = 0.1 };
}

var aBucket = new Bucket {
    FilledWith = ResourceTable.Iron,
    Quantity = 100
};