【问题标题】:C# Inheritance: Use static field? [duplicate]C# 继承:使用静态字段? [复制]
【发布时间】:2011-10-12 14:44:32
【问题描述】:

可能重复:
C# Inheritance: Static vs. Non-Static Field

我正在为控制电路创建一个类库:

private abstract class ControllerBasics
{
   protected SerialPort serial;  // The serial port to communicate with the controller.
   protected Dictionary<int, string> errorDescriptions = new Dictionary<int, string> {{1, "Sensor Error"},{2, "Controller Error"}, ...};   // Possible errors for the controller (known and fixed). Won't change from controller to controller.

   public string SendReceiveCommand(string command){...} // Method to send string command over "serial".       
}

public class OverallController : ControllerBasics // The actual class used to communicate with the controller.
{       
   // Add top-level controller settings.
   private string controllerName = "Controller1"; // Have a property to get/set.
   private bool controllerON; // Controller on/off. Have property to get/set.
   ... // Other similar fields and methods.

   // Used to "sort" the controller's many settings/functions.
   private SensorSettings sensorSettings;  // Have get/set properties for these so I could do the following: overallControllerInstance.GetSensorSettingsProperty.SetActiveSensorCount(5);
   private OutputSettings outputSettings;
   private EnvironmentSettings environmentSettings;

   public OverallController(string name, string comPort, ...)  // Constructor.
   {
      // Basic settings initialization.
      // Create serial port.
      this.sensorSettings = new SensorSettings(this.serial);
      this.outputSettings = ...
}

public class SensorSettings : ControllerBasics // Class to hold the controller's specific sensor settings and their respective get/set methods. Not a new type of controller.
{
   private int activeSensorCount; // Have public method to get/set.
   ... // Others.

  public void SetActiveSensorCount(int sensorCount)
  {
     // Send command using inherited SendReceive().
  }
  ... // Others.
}

public class OutputSettings : ControllerBasics   // Same logic as SensorSettings.
{
   private string units; // Have public method to get/set.
   ... // Others.

  public string GetUnitType()  // Meters, mm, um...
  {
     // Send command using inherited SendReceive().
  }
  ... // Others.
}

public class EnvironmentSettings : ControllerBasics   // Same logic as SensorSettings.
{
   ...
}

因此,如果ControllerBasics 中定义的errorDescriptions 是已知的并且在编译时已修复,我应该将其设为静态还是应该将其保留为受保护并且每个派生类都有自己的字典(即this.errorDescriptions)?如果我将其设为静态,我将如何在派生类中引用它?例如,如果在Sensor Settings 中,我会使用ControllerBasics.errorDescriptions 还是SensorSettings.errorDescriptions

谢谢!

【问题讨论】:

  • 如果您查看大多数新问题,它们很可能是对前一个问题的重复。人们通常不会在发布之前搜索他们的问题。

标签: c# class inheritance static-members


【解决方案1】:

我建议您使用静态方法,它更符合逻辑、更快且内存效率更高。 是的,您可以使用 ControllerBasics.errorDescriptions 或 SensorSettings.errorDescriptions。

【讨论】:

    【解决方案2】:

    两者兼而有之。如果错误消息在编译时已知,您可能最好将两者结合起来 - 将其设为 protected static Dictionary&lt;int, string&gt; errorDescriptions

    然后您可以使用类名或 this.errorDescriptions。

    【讨论】:

      【解决方案3】:

      由于错误至少目前是恒定的,因此我认为将它们设为静态并在所有实例之间共享并没有错。根据使用情况,它可能会引入代码异味,但重构异味很容易。

      在等式的另一边,我会问您实际使用错误消息的频率以及是否将它们存储在实际类之外可能是更好的解决方案。此外,创建自己的异常类会更好吗,这样使用您的库的人可以在不搜索字符串的情况下确定失败的类型。

      只是想法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-02
        • 2013-10-20
        • 2010-11-03
        • 2012-06-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-27
        相关资源
        最近更新 更多