0

假设我有一个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有一个私有设置器,并且您尝试在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();

关于何时应将自定义类型保留为字段与将其设为属性的一些一般准则是什么?

4

2 回答 2

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实例的调用者可以使用无线电,并且可以将事件通知给汽车。见鬼,任何事情都可以被通知收音机的事件。

于 2013-07-16T17:55:32.727 回答
2

您的问题的答案可能有点主观。

关于何时应将自定义类型保留为字段与将其设为属性的一些一般准则是什么?

但是,微软确实在这里提出了一些指导方针。简而言之,如果您想要进行验证或以其他方式控制设置值时设置的方式/内容,或者是否存在副作用,请使用属性。

您可能还想查看此处此处此处

于 2013-07-16T18:01:35.407 回答