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

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

问题描述

我有一个由多个类使用的接口,并且所有类的接口实现都相同.我会把它变成一个基本抽象类,但是它会阻止派生类以后从另一个类继承,这是我需要的.因此,与其在所有类中重新实现相同的接口,我能否以某种方式在一个地方实现接口,例如 .h 文件并将 .h 文件包含在 cs 文件中,以便类实现"接口.

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# 特定的方法来做到这一点.但是,在更一般的意义上,您可以在其自己的标准类文件中提供实现.对于需要实现接口的每个类,您仍然需要声明接口方法 - 但不是将冗长的实现复制并粘贴到每个类中,而是可以对共享的包含"实现进行一行调用.

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天全站免登陆