问题描述
我现在已经四处寻找解决这个问题的方法.我找到了几种可以解决它的方法,但老实说,我没有意识到哪些方法会被认为是正确的"C# 或 OOP 解决方法.我的目标不仅是解决问题,而且是制定一套好的代码标准,我相当确定有一个标准的方法来处理这个问题.
I have looked around some now to find a solution to this problem. I found several ways that could solve it but to be honest I didn't realize which of the ways that would be considered the "right" C# or OOP way of solving it. My goal is not only to solve the problems but also to develop a good set of code standards and I'm fairly sure there's a standard way to handle this problem.
假设我有 2 种类型的打印机硬件,它们各自的类和通信方式:PrinterType1、PrinterType2.
Let's say I have 2 types of printer hardwares with their respective classes and ways of communicating: PrinterType1, PrinterType2.
如果需要,我还希望以后能够添加另一种类型.抽象的一步,它们有很多共同点.作为示例,应该可以向它们中的每一个发送一个字符串.它们都有共同的变量和每个类独有的变量.(例如,一个通过 COM 端口通信并具有这样的对象,而另一个通过 TCP 通信并具有这样的对象).
I would also like to be able to later on add another type if neccessary. One step up in abstraction those have much in common. It should be possible to send a string to each one of them as an example. They both have variables in common and variables unique to each class. (One for instance communicates via COM-port and has such an object, while the other one communicates via TCP and has such an object).
然而,我只想实现所有这些打印机的列表,并且能够通过列表并在所有打印机上执行发送(字符串消息)",无论类型如何.我还想访问两个对象都相同的变量,例如PrinterList[0].Name",但是我也想在某些地方访问特定于对象本身的数据(例如在为一个对象设置 COM 端口名称而为另一个对象设置 IP/端口号的应用程序.
I would however like to just implement a List of all those printers and be able to go through the list and perform things as "Send(string message)" on all Printers regardless of type. I would also like to access variables like "PrinterList[0].Name" that are the same for both objects, however I would also at some places like to access data that is specific to the object itself (For instance in the settings window of the application where the COM-port name is set for one object and the IP/port number for another).
所以,简而言之:
共同点:
- 姓名
- 发送()
特定于 PrinterType1:
Specific to PrinterType1:
- 端口
特定于 PrinterType2:
Specific to PrinterType2:
- 知识产权
例如,我希望对所有对象执行 Send(),而不管对象的类型和存在的对象数量.
And I wish to, for instance, do Send() on all objects regardless of type and the number of objects present.
我已经阅读了多态性、泛型、接口等方面的内容,但我想知道在我看来基本的问题通常如何在 C#(和/或一般的 OOP)中处理.
I've read about polymorphism, Generics, interfaces and such, but I would like to know how this, in my eyes basic, problem typically would be dealt with in C# (and/or OOP in general).
我确实尝试过创建一个基类,但对我来说似乎不太合适.例如,我在基类本身中没有使用字符串发送(字符串消息)"函数.那么,当我一开始就永远不会使用基类中的函数时,为什么还要在派生类中定义一个需要重写的函数呢?
I actually did try to make a base class, but it didn't quite seem right to me. For instance I have no use of a "string Send(string Message)" function in the base class itself. So why would I define one there that needs to be overridden in the derived classes when I would never use the function in the base class ever in the first place?
我真的很感谢任何答案.这里的人似乎知识渊博,这个地方早些时候为我提供了许多解决方案.现在我终于有了一个可以回答和投票的帐户.
I'm really thankful for any answers. People around here seem very knowledgeable and this place has provided me with many solutions earlier. Now I finally have an account to answer and vote with too.
另外解释一下,我还希望能够访问实际打印机类型的对象.例如 PrinterType1 中的 Port 变量,它是一个 SerialPort 对象.我想像这样访问它:PrinterList[0].Port.Open()
To additionally explain, I would also like to be able to access the objects of the actual printertype. For instance the Port variable in PrinterType1 which is a SerialPort object. I would like to access it like:
PrinterList[0].Port.Open()
并可以访问底层端口的全部功能.同时,我想调用对不同对象以相同方式工作的通用函数(但具有不同的实现):
and have access to the full range of functionality of the underlaying port. At the same time I would like to call generic functions that work in the same way for the different objects (but with different implementations):
foreach (printer in Printers)
printer.Send(message)
推荐答案
使用接口的伪示例.
public interface IPrinter
{
void Send();
string Name { get; }
}
public class PrinterType1 : IPrinter
{
public void Send() { /* send logic here */ }
public string Name { get { return "PrinterType1"; } }
}
public class PrinterType2 : IPrinter
{
public void Send() { /* send logic here */ }
public string Name { get { return "Printertype2"; } }
public string IP { get { return "10.1.1.1"; } }
}
// ...
// then to use it
var printers = new List<IPrinter>();
printers.Add(new PrinterType1());
printers.Add(new PrinterType2());
foreach(var p in printers)
{
p.Send();
var p2 = p as PrinterType2;
if(p2 != null) // it's a PrinterType2... cast succeeded
Console.WriteLine(p2.IP);
}
这篇关于C# 一个列表中的不同类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!