假设我有一个Car
类,该类包含一个Radio
对象。看起来像
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
的某个属性有私有的setter,并且您尝试在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();
关于什么时候应该将自定义类型保留为字段而不是将其作为属性,有哪些一般指导原则?
答案 0 :(得分: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
实例的调用者可以使用无线电,并且可以通知汽车事件。地狱,任何事都可以通知收音机的事件。
答案 1 :(得分:2)
你的问题的答案可能有点主观。
关于什么时候应该将自定义类型保留为字段而不是将其作为属性,有哪些一般指导原则?
但是,Microsoft确实建议了一些guidelines here.简而言之,如果您想要进行验证或以其他方式控制设置值时设置的方式/内容,或者是否存在副作用,请使用属性。