C#一个列表中的不同类对象 [英] C# Different class objects in one list

查看:72
本文介绍了C#一个列表中的不同类对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在环顾四周,以找到解决该问题的方法.我发现了几种解决方法,但老实说,我没有意识到哪种方法被认为是解决问题的正确" 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.

比方说,我有两种类型的打印机硬件,它们分别具有各自的类别和通信方式: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).

因此,简而言之,就像:

So, in short something like:

共同点:

  • 名称
  • 发送()

特定于PrinterType1:

Specific to PrinterType1:

  • 端口

特定于PrinterType2:

Specific to PrinterType2:

  • IP

例如,我希望对所有对象都执行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#一个列表中的不同类对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆