2013-07-16 46 views
0

假設我有Car類,並且該類包含Radio對象。它看起來像何時自定義類型屬性vs會員

class Radio 
    { 
     public string Model { get; set; } 
     private List<string> myChannels = new List<string>(); 

     public List<string> PresetChannels 
     { 
      get { return myChannels; } 
      set { myChannels = value; } 
     } 

     private bool radioState; 

     public void ToggleRadio() 
     { 
      if (!radioState) 
       radioState = true; 
      else 
       radioState = false; 
     } 
     private string selectedChannel = string.Empty; 
     // 
     public void SetStation(int radioButton, string channelName) 
     { 
      while (!ValidateRadioButtonNumber(radioButton)) 
      { 
       Console.Write("Index out of range, choose another value: "); 
       radioButton = Convert.ToInt32(Console.ReadLine()); 
      } 

      PresetChannels[radioButton] = channelName; 
      Console.WriteLine("The {0} radio button was set to {1}",radioButton,channelName); 
     } 
     private bool ValidateRadioButtonNumber(int radioButton) 
     { 
      if (radioButton < 0 || radioButton > 5) 
       return false; 
      else 
       return true; 
     } 
     // 
     public void SelectChannel(int radioButton) 
     { 
      while (!ValidateRadioButtonNumber(radioButton)) 
      { 
       Console.Write("Index out of range, choose another value: "); 
       radioButton = Convert.ToInt32(Console.ReadLine()); 
      } 
      selectedChannel = PresetChannels[radioButton]; 
      Console.WriteLine(PresetChannels[radioButton]); 
     } 
     public Radio() 
     { 
      PresetChannels = new List<string>(); 
      PresetChannels.Capacity = 5; 
      //initialize every element in the list at runtime 
      //so the user can set any station they wish 
      for (int i = 0; i < PresetChannels.Capacity; i++) 
      { 
       PresetChannels.Add(string.Empty); 
      } 
     } 

    } 

Car類像

public class Car 
{ 
    public int Year { get; set; } 
    public string Model { get; set; } 
    private Radio radio; 

    public Radio MyRadio { get; set; } 

    //initialize the radio of the car 
    public Car() 
    { 
     radio = new Radio(); 
     MyRadio = new Radio(); 
    } 
    //containment 
    public void SelectStation(int radioButton) 
    { 
     radio.SelectChannel(radioButton); 
    } 
    public void SetStation(int radioButton, string channelName) 
    { 
     radio.SetStation(radioButton, channelName); 
    } 
    public void ToggleRadio() 
    { 
     radio.ToggleRadio(); 
    } 
} 

如果我讓設計與MyRadio類的屬性,那麼什麼是遏制的地步?如果Radio的一個屬性擁有一個私有設置器,並且您試圖在Main方法中設置該值,它將無法編譯,對吧?

  Car c = new Car(); 
      c.SetStation(0, "99.2"); 
      c.SetStation(10, "100"); //works 
      c.SelectStation(120); 
      Car c2 = new Car(); 
      c2.MyRadio.SetStation(0, "99.0");//works 
      Console.ReadLine(); 

什麼是何時應該保持一個自定義類型的字段與使它屬性的一般準則?

+3

如果您希望對其進行外部修改,或者只在內部進行修改,則將其設爲屬性。 – Mansfield

+1

屬性是成員的子集。和田地一樣。 –

+0

[自動實現的getters和setter與公共字段]的可能重複(http://stackoverflow.com/questions/111461/auto-implemented-getters-and-setters-vs-public-fields) – nawfal

回答

3

在這種情況下,看起來有些過火,在車裏有一個Radio,然後提供方法來訪問它。就我個人而言,我只需要一個Radio屬性和一個返回收音機實例的getter,然後調用者可以直接使用收音機。

如果您需要與Car通信在此過程中,你可以這樣做:

public class Radio 
{ 
    public delegate void StationEvent(int channel); 

    private int _station; 

    public int Station 
    { 
     get { return _station; } 
     set 
     { 
      _station= value; 
      if(SetStation != null) 
       SetStation(_station); 
     } 
    } 


    public event StationEvent SetStation; 
    // etc... 
} 


public class Car 
{ 
    private Radio _radio = new Radio(); 

    public Car() 
    { 
     _radio.SetStation += (station) => { /* Set station was called */ }; 
    } 

    public Radio Radio { get { return _radio; } } 
} 

現在Car實例的調用者可以與無線電工作,汽車可以通知事件。地獄,任何事情都可以通知收音機的事件。

+0

我很失望,我沒有收到我的用戶名稱..直到現在。我認爲這是一陣歡鬧:)。回到手頭,你能解釋一下你的意思嗎?「在這種情況下,看起來像是矯枉過正,在你的車裏有一臺收音機,然後提供方法來訪問它。」 – wootscootinboogie

+0

好吧,在汽車上有一個屬性可以暴露無線電,然後有一些(相當羅嗦)的方法來處理它,當讓人們直接使用Radio並避免所有這些方法調用變得非常簡單(imho) 。從邏輯的角度來看,當你在收音機上換臺時,你是直接用收音機還是用車? (我知道一些更復雜的收音機是相當不可或缺的汽車:)) –

+0

這對我來說已經足夠了。嚴格地從可讀性的角度來看,c.MyRadio.SetStation比c.SetStation更適合我 – wootscootinboogie

2

您的問題的答案可能有點主觀。

什麼是關於何時應該保留自定義類型字段與使其成爲屬性的一般性指導原則?

然而,微軟確實提出了一些guidelines here.一言以蔽之使用屬性,如果你想要做的驗證或以其他方式控制的是如何/當你設置的值,如果有副作用設定。

您可能還想檢查here,herehere