代码之家  ›  专栏  ›  技术社区  ›  Irwin M. Fletcher

用嵌套类.NET开发产品类

  •  0
  • Irwin M. Fletcher  · 技术社区  · 16 年前

    我正在寻找帮助来确定我正在构建的类模型是否可以改进。我正在构建的类是一个具有一些属性的简单产品类。

    class clsProducts
    {
        private string _name;
        private double _productionRate;         
    
        //Constructor
        public clsProducts()
        {
            _name = "null";
            _productionRate = 0.0;           
        }
    
        public clsProducts(string name, double productionRate)
        {
            _name = name;
            _productionRate = productionRate;          
        }
    
        //Properties
        public string Name
        {
            get { return _name; }           
        }
    
        public double ProductionRate
        {
            get { return _productionRate; }           
        }
    
    }
    

    我想补充的是,能够为该类中的每个产品提供月度预测值。我可以添加以下内容来完成此操作

    private double _janValue;
    private double _febValue;
    

    等等,但这看起来很混乱。我还考虑创建一个名为ForecastValues的嵌套类,例如

    class clsProducts 
    {
    ...code here....
    
       protected class ForecastValues
       {
           private string name;
           private double forecastValue;
    
           ...other code.....
       }
     }
    

    然而,我不确定这个想法是否可行。有人能给我一个干净处理这个问题的建议吗?

    谢谢你

    4 回复  |  直到 16 年前
        1
  •  5
  •   Mitchel Sellers    16 年前

    这里有一些东西。

    1. 我建议从类名中删除cls-hungarian前缀。
    2. 取决于你的“预测值”。您可以在“product”类上创建一个属性,该属性是一个列表,或者可能是一个字典。我猜你也许能轻松地走字典的路线。
        2
  •  1
  •   Daniel Brückner    16 年前

    我建议只使用数组和索引器。

    public enum Month
    {
        January =  1, February =  2, March     =  3,
        April   =  4, May      =  5, June      =  6,
        July    =  7, August   =  8, September =  9,
        October = 10, November = 11, December  = 12
    }
    
    public class Product
    {
        private readonly String name = null;
        private readonly Double productionRate = 0.0;
        private readonly Double[] productionRateForcast = new Double[12];
    
        public Product(String name, Double productionRate)
        {
            this.name = name;
            this.productionRate = productionRate;          
        }
    
        public String Name { get { return this.name; } }
        public Double ProductionRate { get { return this.productionRate; } }
    
        public Double this[Month month]
        {
            get { return this.productionRateForcast[month - Month.January]; }
            set { this.productionRateForcast[month - Month.January] = value; }
        }
    }
    

    我不确定是否 month - Month.January 需要显式强制转换为 Int32 . 交替地,一个人可以从 January = 0 但这似乎也有点奇怪。

    我也做了一些代码更改。我删除了默认的构造函数,因为在 Product 具有“未初始化”字段的实例,以后不可能更改这些字段。因此,我也将字段设置为只读。最后,我删除了匈牙利符号前缀-这是一个相当过时的编码风格-并转向 Products 进入之内 产品 因为它代表一个产品,而不是一个产品集合。

    更新

    为了赶上字典的想法……我只提供所需的更改。

    private readonly IDictionary<Month, Double> productionRateForcast =
        new Dictionary<Month, Double>();
    
    public Double this[Month month]
    {
        get { return this.productionRateForcast[month]; }
        set { this.productionRateForcast[month] = value; }
    }
    

    这可能是一个更干净的解决方案,然后使用数组。您也可以通过属性而不是索引器来公开字典,但是我认为索引器是一个更干净的解决方案,因为它隐藏了一些实现细节。

    public IDictionary<Month, Double> ProductionRateForcast
    {
        return this.productionForecast;
    }
    

    在所有情况下,用法如下。

    Product myProduct = new Product("Great Product", 0.8);
    
    myProduct[Month.August] = 0.7;
    

    这看起来很奇怪。可以尝试添加 IndexerNameAttribute 对于索引器,但我不确定是否允许写入

    myProduct.ProductionValueForcast[Month.August] = 0.7;
    

    使用支持索引器的语言。因此,我最终倾向于改变主意,如果 指数货币属性 无济于事。

        3
  •  0
  •   Shaun McDonnell    16 年前

    我不认为嵌套类是个好主意。我要做的是创建一个额外的类“ForecastValues”,但将其标记为“Internal Protected”。这样,您就可以在程序集中使用它,但是代码的用户只有在它包含值时才能引用它。

    -肖恩

        4
  •  0
  •   kay.one    16 年前

    这就是我要做的,

    class ClsProducts
    {
        //Constructor
        public ClsProducts()
        {
            Name = "null";
            ProductionRate = 0.0;
        }
    
        public ClsProducts(string name, double productionRate)
        {
            Name = name;
            ProductionRate = productionRate;
        }
    
        //Automatic properties with private setters
        public string Name { get; private set; }
        public double ProductionRate { get; private set; }
    
        //since you basically have key value pair, why not use one?
        public KeyValuePair<String,Double>   Forcast{ get; set; }
    }
    
    推荐文章