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は、ラジオのインスタンスを返すゲッターを持つプロパティがあれば、呼び出し元はラジオを直接操作できます。

このプロセス中にとの通信が必要な場合は、次の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 に答える