我对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类中的其他类中的这些实例设置字段“StatusLED”
private StatusLED statusLED = new StatusLED();
statusLED.RunLed.ReqPinValue = GpioPinValue.Low;我得到以下错误:
错误:键入‘.’包含外部可见字段“.”字段只能由结构公开。
我可以看到它不喜欢下面的行是公共的,我如何能够访问这个实例的参数从另一个类,而不公开它?
public LED RunLed = new LED();发布于 2018-07-19 06:43:06
您可以创建这些属性,而不是字段:
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方法中消除了大量的重复代码。
做完所有这些工作后,请返回并再次查看您的属性。对于那些只应由类本身设置的代码,而不应由使用该类的任何其他代码设置的,请使setters private。
public GpioPin Pin { get; private set; }https://stackoverflow.com/questions/51415810
复制相似问题