C# 从嵌套类访问高级方法
我想为通过串行端口进行通信的外部控制电路创建一个库类。该电路具有内置功能,可使用串行通信获取/设置各种设置(例如发送“SR,HC,01,1,\r”打开传感器1)。有大约 100 个功能分为以下类别:传感器设置、输出设置和环境设置。这是我尝试过的。
public class CircuitController
{
// Fields.
private SerialPort controllerSerialPort;
private SensorSettings sensorSettings;
private OutputSettings outputSettings;
private EnvironmentSettings environmentSettings;
...
// Properties.
// Properties to get sensorSettings, outputSettings, and environmentSettings.
// Methods.
public string SendReceive(string sendCommand) // Send command and receive response.
{
...
}
// Nested classes.
public class SensorSettings
{
// Fields.
// The various sensor settings here.
// Properties.
// Properties to get/set the sensor settings. Note: Get/Set is done through calling one of the following methods.
// Methods.
public double GetSensorUnits(int sensorNumber)
{
...
string commandToSend = String.Format("HE,WL,1,{0}", sensorNumber); // Setup command string.
string commandResponse = SendReceive(commandToSend); // Send command and receive response. ERROR here, cannot access higher level, non-static methods.
// Logic to process commandResponse.
...
}
// Other methods to create, send, and process the circuit's sensor settings "functions".
}
public class OutputSettings
{
// Same logic as SensorSettings class.
}
public class EnvironmentSettings
{
// Same logic as SensorSettings class.
}
}
我认为这样在 CircuitController
类下就不会塞满 100 个方法/属性。例如,我可以使用 get 属性来获取sensorSettings 实例,然后调用所需的方法/属性:CircuitControllerInstance.GetSensorSettingsProperty.GetSensorUnits(1);
。我尝试从嵌套类访问 SendReceive()
时收到编译错误。有办法做到这一点吗?
谢谢!
I want to create a library class for an external control circuit which communicates via serial port. The circuit has built in functions to get/set various settings using serial communication (e.g. sending "SR,HC,01,1,\r" turns sensor 1 ON). There are ~100 functions sorted into the following categories: Sensor Settings, Output Settings, and Environment Settings. Here is what I tried.
public class CircuitController
{
// Fields.
private SerialPort controllerSerialPort;
private SensorSettings sensorSettings;
private OutputSettings outputSettings;
private EnvironmentSettings environmentSettings;
...
// Properties.
// Properties to get sensorSettings, outputSettings, and environmentSettings.
// Methods.
public string SendReceive(string sendCommand) // Send command and receive response.
{
...
}
// Nested classes.
public class SensorSettings
{
// Fields.
// The various sensor settings here.
// Properties.
// Properties to get/set the sensor settings. Note: Get/Set is done through calling one of the following methods.
// Methods.
public double GetSensorUnits(int sensorNumber)
{
...
string commandToSend = String.Format("HE,WL,1,{0}", sensorNumber); // Setup command string.
string commandResponse = SendReceive(commandToSend); // Send command and receive response. ERROR here, cannot access higher level, non-static methods.
// Logic to process commandResponse.
...
}
// Other methods to create, send, and process the circuit's sensor settings "functions".
}
public class OutputSettings
{
// Same logic as SensorSettings class.
}
public class EnvironmentSettings
{
// Same logic as SensorSettings class.
}
}
I figured that this way there won't be 100 methods/properties crammed under the CircuitController
class. I could use a get property to obtain the sensorSettings instance, for example, and then call the desired method/property: circuitControllerInstance.GetSensorSettingsProperty.GetSensorUnits(1);
. I receive a compile error that I am trying to access SendReceive()
from a nested class. Is there a way to do this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嵌套类不会“看到”其宿主中声明的任何内容。
您应该将主机引用传递给任何嵌套类,例如在构造函数中。
A nested class does not "see" whatever is declared in its host.
You should pass a host reference to any nested class, for instance, in the constructor.