【发布时间】:2011-10-12 14:44:32
【问题描述】:
我正在为控制电路创建一个类库:
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