2011-10-12 75 views
0

可能重複:
C# Inheritance: Static vs. Non-Static FieldC#繼承:使用靜態字段?

我對控制電路產生一個類庫工作:

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. 
{ 
    ... 
} 

所以,如果errorDescriptionsControllerBasics定義是已知的和固定的在編譯時我應該將其設爲靜態的,還是應該讓它保護,並且每個派生類都會保留有它自己的字典(即this.errorDescriptions)?如果我將其設爲靜態,我將如何在派生類中引用它?例如,如果在Sensor Settings中我會使用ControllerBasics.errorDescriptionsSensorSettings.errorDescriptions

謝謝!

+0

如果你看看大多數新問題,他們很可能是前一個問題的重演。人們通常在發佈之前不會搜索他們的問題。 –

回答

0

我建議你使用靜態方法,它更邏輯,更快,並且內存效率更高。 是的,你可以使用ControllerBasics.errorDescriptions或SensorSettings.errorDescriptions。

0

兩者。如果錯誤消息在編譯時已知,則可能最好將兩者合併 - 使其生成protected static Dictionary<int, string> errorDescriptions

然後您可以使用類名稱或this.errorDescriptions。

0

由於錯誤至少現在是不變的,所以我沒有看到讓它們靜態並在所有實例之間共享的錯誤。根據使用情況,它可能引入代碼異味,但很容易重構氣味。

在等式的另一方面,我會問你實際使用錯誤消息的頻率以及是否將它們存儲在實際類之外可能是更好的解決方案。此外,製作自己的異常類會更好,因此使用庫可以在不搜索字符串的情況下確定失敗的類型。

只是想法。