在C#中,一个类可以继承另一个类和一个接口吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2059425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In C#, can a class inherit from another class and an interface?
提问by PICyourBrain
I want to know if a class can inherit from a class and an interface. The example code below doesn't work but I think it conveys what I want to do. 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.
我想知道一个类是否可以继承一个类和一个接口。下面的示例代码不起作用,但我认为它传达了我想要做的事情。我想这样做的原因是因为在我的公司,我们制造 USB、串行、以太网等设备。我正在尝试开发一个通用组件/接口,我可以用它来为我们所有的设备编写程序,这将有助于保持我们所有应用程序的常见事物(如连接、断开连接、获取固件)相同。
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.
添加到这个问题:如果 GenericDevice 在不同的项目中,我可以将 IOurDevices 接口放在该项目中,然后如果我添加对第一个项目的引用,让 USBDevice 类实现该接口吗?因为只想引用一个项目,然后根据设备的不同实现不同的接口。
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;
}
}
采纳答案by Mehrdad Afshari
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.
当然,您仍然需要实现接口定义的所有成员。但是,如果基类包含与接口成员匹配的成员,则基类成员可以作为接口成员的实现而无需您再次手动实现它。
回答by David M
No, not exactly. But it can inherit from a class and implementone or more interfaces.
不,不完全是。但它可以从一个类继承并实现一个或多个接口。
Clear terminology is important when discussing concepts like this. One of the things that you'll see mark out Jon Skeet's writing, for example, both here and in print, is that he is always precise in the way he decribes things.
在讨论此类概念时,清晰的术语很重要。例如,在这里和在印刷品中,你会看到 Jon Skeet 的作品标记的一件事是,他描述事物的方式总是精确的。
回答by STW
Unrelated to the question (Mehrdad's answer should get you going), and I hope this isn't taken as nitpicky: classes don't inheritinterfaces, they implementthem.
与问题无关(Mehrdad 的回答应该会让你有所了解),我希望这不会被视为挑剔:类不继承接口,它们实现了它们。
.NET does not support multiple-inheritance, so keeping the terms straight can help in communication. A class can inherit from one superclass and can implement as many interfaces as it wishes.
.NET 不支持多重继承,因此保持术语直白有助于交流。一个类可以从一个超类继承,并且可以实现任意数量的接口。
In response to Eric's comment... I had a discussion with another developer about whether or not interfaces "inherit", "implement", "require", or "bring along" interfaces with a declaration like:
为了回应 Eric 的评论……我与另一位开发人员讨论了接口是否“继承”、“实现”、“要求”或“带来”接口,并带有如下声明:
public interface ITwo : IOne
The technical answer is that ITwo
does inherit IOne
for a few reasons:
技术上的答案是ITwo
继承IOne
的原因有几个:
- Interfaces neverhave an implementation, so arguing that
ITwo
implementsIOne
is flat wrong ITwo
inheritsIOne
methods, ifMethodOne()
exists onIOne
then it is also accesible fromITwo
. i.e:((ITwo)someObject).MethodOne())
is valid, even thoughITwo
does not explicitly contain a definition forMethodOne()
- ...because the runtime says so!
typeof(IOne).IsAssignableFrom(typeof(ITwo))
returnstrue
- 接口从来没有实现,所以认为
ITwo
实现IOne
是完全错误的 ITwo
继承IOne
方法,如果MethodOne()
存在于IOne
那么它也可以从ITwo
. 即:((ITwo)someObject).MethodOne())
是有效的,即使ITwo
没有明确包含定义MethodOne()
- ...因为运行时是这样说的!
typeof(IOne).IsAssignableFrom(typeof(ITwo))
返回true
We finally agreed that interfaces support true/full inheritance. The missing inheritance features (such as overrides, abstract/virtual accessors, etc) are missing from interfaces, not from interface inheritance. It still doesn't make the concept simple or clear, but it helps understand what's really going on under the hood in Eric's world :-)
我们最终同意接口支持真正/完全继承。缺少的继承特性(例如覆盖、抽象/虚拟访问器等)是接口中缺少的,而不是接口继承中缺少的。它仍然没有使概念变得简单或清晰,但它有助于了解 Eric 世界中真正发生的事情:-)
回答by PICyourBrain
I found the answer to the second part of my questions. Yes, a class can implement an interface that is in a different class as long that the interface is declared as public.
我找到了问题第二部分的答案。是的,只要接口被声明为 public,一个类就可以实现不同类中的接口。