我可以在.h文件中的C#中实现接口吗? [英] Can I implement an interface in a C# in an .h file?

查看:176
本文介绍了我可以在.h文件中的C#中实现接口吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由几个类使用的接口,并且所有类的接口实现都是相同的。我会把它作为一个基本的抽象类,但是它会阻止派生类稍后从另一个类继承,这是我需要的。因此,不是在所有类中重新实现相同的接口,我可以在某个地方实现接口,如.h文件,并在.cs文件中包含.h文件,以便类实现接口。

I have an interface that is used by several classes and the interface implementation is the same for all the classes. I would make it a base abstract class, but then it would stop the deriving classes from inhering from another class later on, which I need. So instead of reimplementing the same interface in all the classes, can I somehow implement the interface in one place, like an .h file and include the .h file in the cs file so the class "implements" the interface.

顺便说一下,界面主要由属性组成,并在委托上更改。

By the way, the interface consists mostly of properties and changed on delegates.

推荐答案

我的C#上有点生疏,所以肯定会有一种特定于C#的方法。但是,从更一般的意义上讲,您可以在自己的标准类文件中提供实现。对于需要实现接口的每个类,您仍然需要声明接口方法 - 但不是将冗长的实现复制并粘贴到每个接口中,您可以对共享的include实现进行单行调用。

I'm a little rusty on my C#, so there may certainly be a C#-specific way of doing this. However, in a more general sense, you could provide the implementation in its own, standard class file. For each class that needs to implement the interface, you would still need to declare the interface methods - but instead of having lengthy implementations copied-and-pasted into each, you could have one-line calls to the shared "include" implementation.

(这可能不是最好的解决方案,但应该是一个可行的,至少可以让你开始。)

(This probably won't be the best solution, but should be a workable one that could at least get you started.)

这篇关于我可以在.h文件中的C#中实现接口吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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