- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我一直在努力让自己的注意力集中在类里面。即使在阅读了几个网站和书籍之后,我仍然不觉得我真的“明白”了。
我正在处理的应用程序有一个 sub,它将通过 USB 到 GPIB 接口(interface)控制几个不同的测试设备。
所以,我有:
Laptop USB - GPIB Interface Power Supply (Device Address 5) HP34970 Datalogger (Device Address 6)
在我的潜艇中,我想打开 GPIB 设备,发送一些 SCPI 命令以打开电源,再发送一些命令到 HP34970 以切换继电器或测量电压。
看起来很简单,我可以很容易地让所有东西都在同一个模块中工作。然而,我认为为 GPIB 接口(interface)、电源和 HP34970 提供一个单独的类会好得多。如果是这样的话,我可以很容易地在其他项目中重用代码。
这是我无法获得心智模型的地方——如果我创建了 GPIB 类的实例并有一个方法来“打开”到 GPIB 总线的 channel ,我如何允许其他类中的方法(比如电源)使用在 GPIB 类中创建的开放 channel ?例如电源类中设置电压的方法。
如果有人可以花几分钟时间发布伪代码和一些解释来说明我可以/应该如何组织它,我将不胜感激!
谢谢
最佳答案
将类视为设备本身。这里的主要好处是能够重用一次编写的代码——例如,你所有的设备都有地址,它们可以连接、测试连接、断开连接——为什么不把它放在一个抽象的“设备”类中,并使所有设备从中继承?
这可能是其中一种用法 - 用 C# 编写(抱歉,如果我误解了实际用法:) 这里我假设电源和 dataLogger 连接到接口(interface),接口(interface)连接到笔记本电脑)。
public class DeviceTesting
{
private string powerSupplyAddress = "DA5";
private string dataLoggerAddress = "DA6";
public static void main()
{
//bring interface into life
DeviceInterface interface = GPIBInterface(); //example of inheritance - GPIB could be just one of the many device interfaces
//the constructor in PowerSupply takes on the responsibility of initializing itself
PowerSupply ps = new PowerSupply(powerSupplyAddress, interface); //plugged in with interface - has a reference to it
//you could have multiple types of data loggers - all could inherit from DataLogger, and here so does HP34970
//This means you can reuse the common code for data loggers, and write only the specifics for each specific device of that kind
DataLogger myLogger = new HP34970(dataLoggerAddress, interface);
//now, do whatever you do with the real devices
ps.SetVoltage(220); //send voltage command directly
interface.SendLogMessage("Interface is operational");
interface.ExecuteTest("test1"); //send voltage command through interface
//etc... you get the idea...
}
}
现在,由于接口(interface)知道它所连接的设备,它必须将它们包含在它的构造函数中(在面向对象的设计中,也称为依赖注入(inject),更具体地说,这里是构造函数注入(inject)):
public GPIBInterface : DeviceInterface //here we are reusing code that's same for all interfaces
{
//whoever attaches to this interface is responsible for registering itself with it
public PowerSupply Power{get;set;}
public DataLogger Logger{get;set;}
//notice here that it can work with any class that inherits from DataLogger
public GPIBInterface()
{
}
private override void TestInitialization() //as an example, we write our own testing by overriding some method in base class
{
base.TestInitialization(); //make sure that the base class checks it's ok - e.g. if it's own display is working or similar...
if (Power.TestConnection() == false)
throw new DeviceNotWorkingException(ps);
if (Logger.TestConnection() == false)
throw new DeviceNotWorkingException(_logger);
}
public void SendLogMessage(string message)
{
Logger.SendMessage(message);
}
public void ExecuteTest(string testName)
{
switch(testName)
{
case "test1":
Power.SetVoltage(280);
Logger.SendMessage("Voltage increased to 280V");
break;
}
}
}
基本上,如果您的设备在“现实生活”中相互交互,则这意味着它们应该具有对所连接的其他设备的引用。例如,如果您将 PowerSupply 直接插入到 logger,那么 PowerSupply 类应该有对 logger 的引用。但是,如果它连接到接口(interface),然后连接到记录器,电源必须只引用接口(interface)而不是记录器(因为它没有连接到它)。
我希望你明白这个想法——它并不是无缘无故地被称为“面向对象”——将类视为真实的对象——如果一个对象可以做 thing1,那么你应该在你的类中有方法“thing1()”;如果对象与 object441 有连接,那么该类应该有对该类的引用。只要遵循这个原则,看看它会把你带到哪里......
当你掌握它时,你的程序将真正受益于一些控制反转 (IoC) 模式 - 这样你只需在开始时定义什么是记录器,什么是电源,谁作为显示器执行,以及何时有人需要 IoC 容器提供的特定设备。这样,您就不必在每次有新设备出现时都重写代码 - 只需向 IoC 容器注册新类型,一切正常。
关于vb.net类组织,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3613547/
我有几个问题。我是 Visual Basic 这个领域的新手,所以不要取笑我。 1.) VB.NET之间有什么区别和 VB ? 2.) 我需要为 Windows 开发基本的应用程序。(如记事本)我应该
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我是框架 3.5 的新手。我注意到,在创建 Web 内容表单时,除了 aspx.vb 页面之外,它还会创建一个 aspx.designer.vb 页面。谁能向我解释一下它们之间的区别以及它们的用途吗?
我只是想知道 VB.NET 和 VB 2010 是否相同。 我只是想知道。 最佳答案 VB 2010 是 VB.Net 的最新版本。 Microsoft 在 VB 2005 版本中删除了 VB 的“.
我是框架 3.5 的新手。我注意到,在创建 Web 内容表单时,除了 aspx.vb 页面之外,它还会创建一个 aspx.designer.vb 页面。谁能向我解释一下它们之间的区别以及它们的用途吗?
我正在尝试将 VB 函数移植到 VB.NET,但我无法使该函数正常工作并正确更新。 rFormat = Format(Format(Value, fmt), String$(Len(fmt), "@"
如何在VB中注释多行代码/代码块? 最佳答案 VB 在语言级别上没有这样的构造。它有使用撇号字符的单行注释: ' hello world ' this is a comment Rem this is
我正在使用我在 VB2005 中创建的表单在按下按钮时打开程序,然后在文本字段中显示进程 ID(再次按下按钮时)。当我运行它时,表单将打开程序 (Notepad.exe) 但当我单击按钮查看进程 ID
我正在尝试添加一个从 vb.net 创建的 dll,并且想将其导入到现有的 vb 6 项目中,但它给了我错误“无法添加对指定文件的引用”。 。有人知道如何解决这个问题吗? 最佳答案 需要遵循以下步骤:
我有一个数据 GridView 。右键单击它会显示一个上下文菜单,但它始终位于右上角。我想要它,以便菜单出现在用户右键单击的单元格上。它可能是单元格 1 或单元格 2 或其他。 谢谢福尔坎 最佳答案
我只是在 Visual Studio 2010 中使用 Visual Basic。有人知道我将如何制作“浏览文件夹(或文件)”按钮吗?我对 VB 真的很陌生,我只是在寻找一些简单的帮助:) 最佳答案
这次感到困惑... 最简单的代码行有时可能起作用,有时却没有。首先,我认为问题在于我试图读取DWORD的值,但是由于我可以从某些键读取DWORD值,所以这一定不是问题。现在的问题似乎是,如果 key
我的代码中有此方法: Private Sub Display() Received.AppendText(" - " & RXArray) End Sub 这两个调用之间有什么区别:
我正在创建一个宏程序来记录和回放鼠标和键盘输入。录制效果很好,鼠标播放也一样,但是我在播放键盘输入时遇到了麻烦——特别是在释放之前按住一个键几秒钟。这不等同于重复按键。这是我尝试过的: 技巧 1:Me
我最近刚刚了解了 VB.NET 中静态局部变量的使用,并想知道它在延迟加载属性中的潜在用途。 考虑以下示例代码。 Public Class Foo Implements IFoo End Clas
VB 有一个 C# 没有的特性,在项目级别导入命名空间(我的项目>引用>导入命名空间)。当新人在源代码控制之外检查项目时,我们的自定义导入不包括在内。这个 VB 特定的导入命名空间存储在哪里? 最佳答
我已将我的问题缩小到这个简单的案例,但似乎无法找到发生了什么: 我有两个表单,一个只有一个按钮,另一个是空的。 单击按钮时,form1 隐藏和显示 form2 出现时,form2隐藏,form1再次显
为什么下面的简单代码会失败?无论我使用 LinearGradientMode 的哪个值,这段代码总是用从左到右的渐变填充路径。 graphPath 是在别处创建的 GraphicPath 对象(基本上
我可以多快替换字符串中的字符? 所以这个问题的背景是这样的:我们有几个应用程序通过套接字相互通信并与客户端的应用程序通信。这些套接字消息包含不可打印的字符(例如 chr(0)),需要用预定的字符串(例
如何从任何文件中读取原始字节数组... Dim bytes() as Byte ..然后将该字节数组写回新文件? 我需要它作为一个字节数组来做一些处理。 我目前正在使用: 阅读 Dim fInfo
我是一名优秀的程序员,十分优秀!