在C#中,可以在一个类从另一个类和接口继承? [英] In C#, can a class inherit from another class and an interface?

查看:519
本文介绍了在C#中,可以在一个类从另一个类和接口继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道,如果一个类可以从类和接口继承。
下面的实例代码不工作,但我认为它传达什么,我想做的事情。
没有人有什么建议?我想这样做的原因是因为在我的公司,我们做的USB,串口,以太网等设备。我想开发一个通用组件/接口,我可以用它来写我们所有的设备上的程序,这将有助于保持共同的东西(如连接,断开连接,获取固件)同样为所有的应用程序...任何建议, !是appriciated



要添加到这样一个问题:如果GenericDevice是在不同的项目,我可以把该项目的IOurDevices接口,然后再做出USBDevice类实现,如果接口我加入的第一个项目的参考?因为想只引用一个项目,然后实现不同的接口这取决于该设备是...

 类GenericDevice 
{
私人字符串_connectionState;
公共connectionState
{
{返回_connectionState; }
集合{_connectionState =值;}
}
}

接口IOurDevices
{
无效connectToDevice();
无效DisconnectDevice();
无效GetFirmwareVersion();
}

类USBDevice:IOurDevices:GenericDevice
{
//这里我定义的接口
方法//像这样...
无效connectToDevice()
{
connectionState =连接;
}
}

//所以,在我的主程序,我可以做到这一点...

类myProgram
{
的main()
{
USBDevice myUSB =新USBDevice();
myUSB.ConnectToDevice;
}
}


解决方案

是的。尝试:

 类USBDevice:GenericDevice,IOurDevice 

注意:的基类应该接口名称的列表之前



当然。你仍然需要实现所有的接口定义的成员。但是,如果基类包含匹配的接口成员成员,基类成员可以作为接口成员的实现工作,你不需要再手动执行它。


I want to know if a class can inherit from a class and and interface. The example code below doesn't work but I think it conveys what I want to do. Does anyone have any recommendations??? The reason that I want to do this is because at my company we make USB, serial, Ethernet, etc device. I am trying to develop a generic component/interface that I can use to write programs for all our devices that will help keep the common things (like connecting, disconnecting, getting firmware) the same for all of our applications... Any suggestions would be appriciated!

To Add to this question: If GenericDevice is in different project, can I put the IOurDevices interface in that project then then make the USBDevice class implement the interface if I add a reference to the first project?? Because would like to just reference one project and then implement different interfaces depending on what the device is...

class GenericDevice
{
   private string _connectionState;
   public connectionState
   {
      get{return _connectionState; }
      set{ _connectionState = value;}
   }
}

interface IOurDevices
{
   void connectToDevice();
   void DisconnectDevice();
   void GetFirmwareVersion();
}

class USBDevice : IOurDevices : GenericDevice
{
   //here I would define the methods in the interface
   //like this...
   void connectToDevice()
   {
       connectionState = "connected";
   }
}

//so that in my main program I can do this...

class myProgram
{
   main()
   {
      USBDevice myUSB = new USBDevice();
      myUSB.ConnectToDevice;
   }
}

解决方案

Yes. Try:

class USBDevice : GenericDevice, IOurDevice

Note: The base class should come before the list of interface names.

Of course, you'll still need to implement all the members that the interfaces define. However, if the base class contains a member that matches an interface member, the base class member can work as the implementation of the interface member and you are not required to manually implement it again.

这篇关于在C#中,可以在一个类从另一个类和接口继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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