C#inheritance:静态与非静态字段

我有一个库基类( Controller )和三个inheritance自ControllerSensorOutputEnvironment )的子类,其中:

 public class Controller { private SerialPort serial; private Sensor sensorSettings; private Output outputSettings; private Environment environmentSettings; protected Dictionary ErrorDescriptions { get{ this.errorDescriptions; } } protected SerialPort ControllerSerialPort { get{ this.serial; } } protected Sensor SensorSettings { get{ this.sensorSettings; } } // The other sub-class get properties. protected string SendReceive(string controllerCommand) { ... } ... } public class Sensor : Controller {...} ... // Other sub-classes 

我的问题是:我应该将errorDescriptions静态吗? 这些错误描述不会从控制器更改为控制器(即静态),但我不确定inheritance的类中会发生什么。 我是否必须在Sensor类中将它们称为Sensor.errorDescription ,还是仍然是Controller.errorDescription

编辑

哇,我刚刚意识到我搞砸了很多时间。 应该是这样的,我想:

 private abstract class ControllerBasics { protected SerialPort serial; // The serial port to communicate with the controller. protected Dictionary errorDescriptions = new Dictionary {...}; // Possible errors for the controller (known and fixed). Won't change from controller to controller. public enum Sensors {One, Two, ...}; // Possible sensor selection. public string SendReceiveCommand(string command){...} // Method to send string command over "serial". } public class OverallController : ControllerBasics // The controller class. { // 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. { ... } 

因此,如果errorDescriptions定义的errorDescriptions是已知的并且已修复(并且在所有派生类中都需要),我应该将其设置为静态,还是应该保护它,并且每个派生类都将拥有它自己的字典(即this.errorDescriptions)?

只有一个静态变量,但是你有很多子类。 它是否意味着根据子类而变化,还是真正的全局映射? 如果是后者,那么静态变量是合适的。 如果不是……好吧,有各种选择,但你需要告诉我们你正在使用地图。

如果您只需要一个字典实例而不是yes,请将其更改为protected static。 你也应该使用ConcurrentDictionary代替线程安全。

在派生类中,您可以使用Controller.errorDescription访问该字段

好吧,派生类不会显示私有成员。 你需要一个受保护的成员或财产。 在类中,您可以将其称为errorDescriptions或this.errorDescriptions。

我非常警惕有一个静态的,受保护的成员变量,它不是线程安全的,可以被派生类变异。 那只是在惹麻烦。