【问题标题】:in C#, can a derived value be referred to by both static and instance methods/properties?在 C# 中,静态和实例方法/属性可以引用派生值吗?
【发布时间】:2015-03-01 17:49:53
【问题描述】:

我对 C# 还是很陌生,我一直在拼命想弄清楚如何实现与从派生类返回相同值的实例和静态属性相关的某种设计模式。似乎有一些限制可以防止这种情况发生,所以我想解释一下我的问题,看看是否有人对如何解决这个问题有想法。

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

这段代码非常不工作(总是返回基类 Resource 的值),但我这样写是为了表明我希望做什么......有一个可以被两者引用的值:

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

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

【问题讨论】:

    标签: c# oop static


    【解决方案1】:

    如果这是真的想要的,那么

    // 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. 通常,单例不允许“替代创建方法”;可变单例是 .. 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
    };
    

    【讨论】:

    • 感谢您的快速响应...由于您的解决方案是一起消除静态 GetX() 方法,我猜想得出的结论是基本静态方法不能引用支持字段集通过派生类?
    • 静态只能指静态;和静态字段不受子类型多态性的影响。
    • 好的,这完全回答了我的问题。我希望避免使用“ResourceTable”类型的类,但是由于无法轻松使用静态作为替代,您的解决方案对我来说很有意义。感谢您的帮助,以及从数量中拆分资源的建议。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-03
    • 2012-04-15
    • 2016-11-10
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多