【问题标题】:Exposing fields in a class在类中公开字段
【发布时间】:2018-07-19 06:32:25
【问题描述】:

我是 C# 和对象编码的新手,所以请温柔....

我有一个班级呼叫 LED,见下文:

public sealed class LED
{
    public GpioPinValue ReqPinValue { get; set; }     //Enable/Disable LED
    public bool Flashing { get; set; }      //Does the LED flash
    public GpioPin Pin { get; set; }
    public int flashingPeriod { get; set; } //Period to flash in seconds



    private GpioPinValue value; //Pin value (high/low)
    private int flashCount = 0; //Times we have entered the timer loop


    public LED()
    {
    }

    public void UpdateLED()
    {
        int timesToCycle = 0;

        if (ReqPinValue == GpioPinValue.Low)
        {
            if (Flashing)
            {
                timesToCycle = flashingPeriod * 2;
                if (flashCount == timesToCycle)
                {
                    value = (value == GpioPinValue.High) ? GpioPinValue.Low : GpioPinValue.High;
                    Pin.Write(value);
                    flashCount = 0;
                }
                else
                    flashCount++;
            }
            else
            {
                Pin.Write(GpioPinValue.Low);
            }
        }
        else
        {
            Pin.Write(GpioPinValue.High);
        }
    }

}

在另一个类中,我为 4 个不同的状态 LED 创建了该 LED 类的四个实例。

public sealed class StatusLED
{
    private const int RUN_LED = 4;
    private const int IO_LED = 17;
    private const int NET_LED = 27;
    private const int FAULT_LED = 22;

    public LED RunLed = new LED();
    public LED IOLed = new LED();
    public LED NetLed = new LED();
    public LED FaultLed = new LED();

    private GPIO GPIO = new GPIO();
    private GpioController gpioController;

    private ThreadPoolTimer timer;


    public void InitStatusLED()
    {
        gpioController = GPIO.InitGPIO();

        if (gpioController == null)
        {
            Debug.WriteLine("Failed to find GPIO Controller!");
            //TODO proper error handling although this should never happen
        }
        else
        {
            //Setup the default parameters for the LEDS (ie flashing or non-flashing)
            RunLed.Flashing = false;
            IOLed.Flashing = false;
            NetLed.Flashing = false;
            FaultLed.Flashing = false;

            RunLed.flashingPeriod = 0;
            IOLed.flashingPeriod = 0;
            NetLed.flashingPeriod = 0;
            FaultLed.flashingPeriod = 0;

            RunLed.Pin = GPIO.InitOutputPin(gpioController, RUN_LED);
            IOLed.Pin = GPIO.InitOutputPin(gpioController, IO_LED);
            NetLed.Pin = GPIO.InitOutputPin(gpioController, NET_LED);
            FaultLed.Pin = GPIO.InitOutputPin(gpioController, FAULT_LED);

            //Turn the LED's on to Start
            RunLed.ReqPinValue = GpioPinValue.Low;
            IOLed.ReqPinValue = GpioPinValue.Low;
            NetLed.ReqPinValue = GpioPinValue.Low;
            FaultLed.ReqPinValue = GpioPinValue.Low;

            timer = ThreadPoolTimer.CreatePeriodicTimer(Timer_Tick, TimeSpan.FromMilliseconds(500));
        }
    }
    private void Timer_Tick(ThreadPoolTimer timer)
    {
        RunLed.UpdateLED();
        IOLed.UpdateLED();
        NetLed.UpdateLED();
        FaultLed.UpdateLED();
    }
}

我现在想使用下面的代码从另一个类中为 StatusLED 类中的这些实例设置字段“ReqPinValue”

private StatusLED statusLED = new StatusLED();

statusLED.RunLed.ReqPinValue = GpioPinValue.Low;

我收到以下错误:

错误:类型“....”包含外部可见字段“....”字段 只能通过结构暴露。

我可以看到它不喜欢下面的行被公开,我怎样才能在不公开的情况下从另一个类访问这个实例的参数?

public LED RunLed = new LED();

【问题讨论】:

    标签: c# class parameters


    【解决方案1】:

    您可以制作这些属性而不是字段:

    public LED RunLed {get;set;}
    public LED IOLed {get;set;}
    public LED NetLed {get;set;}
    public LED FaultLed {get;set;}
    

    然后,在您的 InitStatusLED 方法中,初始化它们:

    public void InitStatusLED()
    {
        gpioController = GPIO.InitGPIO();
    
        if (gpioController == null)
        {
            Debug.WriteLine("Failed to find GPIO Controller!");
            //TODO proper error handling although this should never happen
        }
        else
        {
            //Setup the default parameters for the LEDS (ie flashing or non-flashing)
            RunLed = new LED(GPIO.InitOutputPin(gpioController, RUN_LED));
            IOLed = new LED(GPIO.InitOutputPin(gpioController,IO_LED));
            //And so on
    

    但是等等 - 你的 LED 类没有接受 GpioPin 的构造函数。让我们也解决这个问题:

    public sealed class LED
    {
        public GpioPinValue ReqPinValue { get; set; }     //Enable/Disable LED
        public bool Flashing { get; set; }      //Does the LED flash
        public GpioPin Pin { get; set; }
        public int flashingPeriod { get; set; } //Period to flash in seconds
    
    
    
        private GpioPinValue value; //Pin value (high/low)
        private int flashCount = 0; //Times we have entered the timer loop
    
    
        public LED(GpioPin pin)
        {
            Pin = pin;
            Flashing = false;
            flashingPeriod = 0;
            ReqPinValue = GpioPinValue.Low;
        }
    

    看看我们是如何从InitStatusLed 方法中消除一大堆重复代码的。

    完成所有这些后,请返回并再次查看您的属性。对于那些应该由类本身设置,而不是由任何其他代码使用类设置的设置,设置器private

    public GpioPin Pin { get; private set; }
    

    【讨论】:

      猜你喜欢
      • 2021-12-16
      • 2011-05-09
      • 2021-01-09
      • 2020-04-03
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      相关资源
      最近更新 更多