"摘要" C#中的接口 [英] "Abstract" interface in C#

查看:112
本文介绍了"摘要" C#中的接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个学术问题。
可能存在后面的X-Y问题,我可以稍后单独发布。但我 am 实际上对学术问题特别感兴趣,这里。

This is an Academic question. There is arguably an X-Y problem behind it, which I may post separately later. But I am actually specifically interested in the Academic Question, here.

我经常发现我有一组接口,它们都有共同的属性。我想要定义一个基本接口,以便将它们共同化,部分原因是为了避免重复,部分原因是我可以在不知道确切类型的情况下传递对象并使用常用方法。

I often find that I have groups of interfaces which all have properties in common. And I want to define a base interface to commonise those, partly for lack of repetition and partly so that I can pass around an object and use the common methods without knowing the exact type.

也许我有 IFooRepository IBarRepository 等,我可以声明 IRepository< ; TEntity>

Maybe I have IFooRepository, IBarRepository, etc., and I can declare IRepository<TEntity>.

或者我有 IHappyBot ISadBot IConfusedBot ,所有这些都有 IBot 的共同点。

Or I have an IHappyBot, ISadBot, IConfusedBot, all of which have IBot in common.

值得注意的是,没有类可以直接实现这些基本接口 - 你永远不会有一些实现只是 IBot

Notably no class would ever directly implement these base interfaces - you'd never have something that implemented just IBot.

如果我们讨论的是的层次结构,而不是接口,那么我会说啊......基本的东西是一个抽象类。

If we were talking about a hierarchy of classes, rather than interfaces, then I would say "Ah ... the base thing is an abstract class".

有什么类似的东西我可以用接口来记录期望 IBot 不会直接实现。

Is there anything analogous that I can do with the interface to document the expectation that IBot isn't going to get directly implemented.

我感兴趣的一个方面是做一些你以后可以通过反射检测的东西,这样当我测试我的DI设置,我可以说啊,这个界面不是预期的是可绑定的,因为它是抽象的。

A aspect of it that I'm interested is doing something that you can later detect via reflection, so that when I test my DI setup, I can say "Ah, this interface isn't expected to be bindable, because it's "abstract".



我主要关心C#,但如果这个功能特别存在于其他主要语言中,那么听到它会很有趣。 / p>


I mainly care about C# myself, but if this feature specifically exists in other major languages it would interesting to hear about it.

推荐答案

或许是一个回答的哲学问题 - 但是为什么一个班级如果不想实施IBot呢?

A philosophical question in response perhaps - But why should a class not be able to implement IBot if it wants to?

抽象类怎么样?我可能想要一个抽象基础Bot类来实现IBot,以检查Bot基类是否记录了基本Bot所需的所有功能。

What about an abstract class? I might want an abstract base Bot class to implement IBot as a way to check that the Bot base class ticks all the functionality expected of a base Bot.

接口是关于定义什么可以/应该做什么,它是一个功能列表。在我看来,说某些东西不能声称它满足这个功能列表没有多大意义。

An interface is about defining what something can / should do, it's a list of functionality. In my mind it doesn't make much sense to say "something can't claim it satisfies this list of functionality".

抽象类是有意义的,因为有时抽象class需要填充实现漏洞(抽象方法等)。接口不是这种情况。

An abstract class makes sense because sometimes the abstract class needs its implementation holes filled (abstract methods etc). This isn't the case with an interface.

这篇关于&QUOT;摘要&QUOT; C#中的接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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