c# - 接口(interface) : simplified

标签 c# java javascript c++ interface

我一直在对接口(interface)进行一些研究,并对它的真正含义进行简单的外行解释。当在浩瀚的书海中搜索时出于某种原因,人们喜欢使用过于复杂的解释和行话来解释真正简单的概念(我猜这会让他们觉得自己很大),我有一种直觉,在这种情况下也是如此。

所以据我所知,接口(interface)似乎只不过是一种保留方法名称、它们的返回类型(如果有)以及它们接受的参数的类型和数量的方法。因此,当一个类实现一个接口(interface)(或多个接口(interface))时,它被迫从接口(interface)定义每个方法的主体。我是对这个感兴趣还是需要继续挖掘?

附注我知道 javascript 不支持接口(interface),但我仍然需要理解这个概念,因为有很多地方展示了如何在一定程度上模拟。

最佳答案

For some reason people love using overly complex explanations and jargon to explain truly simple concepts (guess it makes them feel big)

请考虑避开将不良动机归咎于试图帮助您的人的社论评论。 这种试图让人们帮助您的方式非常糟糕。

It seems like interfaces are nothing more than a way to reserve method names, their return type if any, and the type and number of arguments they require. So when a class implements an interface (or interfaces) it is forced to define the body of each method from the interface(s). Am i on the nose with this one or do i need to keep digging?

你走在正确的轨道上,但你在细节上犯了错误。例如,在 C# 中,不需要实现类来提供主体。例如,与接口(interface)方法相对应的方法可以是抽象类中的抽象方法,这样就没有主体了。在 C# 中,接口(interface)可以需要方法以外的成员;例如属性、事件和索引器。

一种更简洁和典型的方式来表达接口(interface)强加要求的想法,类型提供成员匹配某些 signatures 是说接口(interface)代表一个契约(Contract),必须由其实现者履行。但这对于您的直觉来说可能过于复杂和特化。

关于c# - 接口(interface) : simplified,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9809533/

相关文章:

c# - WPF TreeView 和虚拟化(UI 和数据)

java - 使用 Box2D(ligdx 修改)类在 Libgdx 中创建一个 polygonShape,但该对象没有碰撞

javascript - Meteor 应用程序 - 主干路由器未被调用

java - 如何根据多个变量制作随机二维数组?

java - 难以理解代码,特别是 &

javascript - 如何修复 Vue js 3 上的 "Vue packages version mismatch"错误

使用 try .. catch .. finally 处理 Javascript 错误

c# - .NET : How do you get the Type of a null object?

c# - LINQ to SQL - 数据库设计问题

c# - SubmitChanges 立即取回记录