接口和继承方面的差异有哪些用途
接口在设计应用程序时的主要优点是什么以及与继承方面的差异。任何人都可以给我提供一个简短的例子吗?
What are the major advantages of interfaces in designing application and the differences with respect to inheritance. can any body provide me with the brief example.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对象通过它们公开的方法定义它们与外部世界的交互。方法形成对象与外界的接口;例如,电视机正面的按钮是您与其塑料外壳另一侧的电线之间的接口。您按下“电源”按钮即可打开和关闭电视。
在最常见的形式中,接口是一组具有空主体的相关方法。
如果将自行车的行为指定为接口,则可能如下所示:
要实现此接口,类的名称将发生变化(例如,更改为特定品牌的自行车,例如 ACMEBicycle),并且您将使用实现类声明中的关键字:
实现接口允许类对其承诺提供的行为变得更加正式。接口形成类和外部世界之间的契约,并且该契约由编译器在构建时强制执行。如果您的类声明实现一个接口,则该接口定义的所有方法都必须出现在其源代码中,该类才能成功编译。
您还可以找到更多详细信息,检查
Interface
和Class
之间的差异。Objects define their interaction with the outside world through the methods that they expose. Methods form the object's interface with the outside world; the buttons on the front of your television set, for example, are the interface between you and the electrical wiring on the other side of its plastic casing. You press the "power" button to turn the television on and off.
In its most common form, an interface is a group of related methods with empty bodies.
A bicycle's behavior, if specified as an interface, might appear as follows:
To implement this interface, the name of your class would change (to a particular brand of bicycle, for example, such as ACMEBicycle), and you'd use the implements keyword in the class declaration:
Implementing an interface allows a class to become more formal about the behavior it promises to provide. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler. If your class claims to implement an interface, all methods defined by that interface must appear in its source code before the class will successfully compile.
You can find more details also checking difference between
Interface
andClass
.接口和类之间的唯一区别是:
The only differences between interfaces and classes is:
接口最好用在以下地方:
Interfaces are best used in places where:
使用接口的一个优点是您可以在单元测试期间使用模拟对象。
例如,当一个方法需要 DbConnection 实例时,您必须提供它 - 这在小型测试中可能非常困难。但当它需要
IDbConnection
时,您可以提供模型。SO 上有一个标签“mocking”。
An advantage of using interfaces is that you can use mocked objects during your unit tests.
E.g. When a method requires a
DbConnection
instance, you have to provide it - which can be very difficult within a small test. But when it requires aIDbConnection
you can provide a mockup.There is a tag "mocking" on SO.
接口广泛应用于许多高级设计模式中。特别是当应用程序设计使用任何类型的依赖注入或控制反转时。
例如,插件(附加)模型通常使用接口来定义插件。将有一个严格版本控制的互操作库 (DLL),它定义了应用程序使用的任何插件可以/应该/必须实现的接口。
应用程序项目引用互操作库,这允许它使用任何实现这些接口的类,而不需要直接引用实际的类。
插件的具体实现引用互操作库,以便其中的类可以实现所需的接口。应用程序对插件库没有直接的了解,插件库对应用程序也没有直接的了解。它们只能通过互操作接口进行通信。
在插件场景中,应用程序通常会指定一个插件所在的文件夹。在运行时,它扫描文件夹中的程序集,扫描每个程序集的类/结构,检查每个类/结构中的已知(互操作)接口,并动态加载它找到的任何匹配插件< em>通过接口。根据机制,每个插件通常会添加到某个菜单中,以便最终用户可以使用。
在这种模型中,插件可以更新而无需重新编译应用程序,并且应用程序也可以更新而无需重新编译插件。 只要互操作库版本/签名不更改,应用程序本身或单个插件就可以独立更新/修改/修复,而无需重新分发整个 kit-n-kaboodle。
此外,将为您的应用程序开发插件的第三方只需专注于实现互操作中的特定接口,而不必关心您的应用程序如何使用它们的具体细节。
Interfaces are used extensively in many advanced design patterns. Especially if the application design uses any sort of Dependency Injection or Inversion of Control.
For instance, a plug-in (add-on) model typically uses interfaces to define the plugin. There will be an interop library (DLL), strictly versioned, which defines the interfaces that can/should/must be implemented by any plug-ins to be consumed by the app.
The application project references the interop library, which allows it to consume any classes that implement those interfaces, without requiring direct references to the actual classes.
The concrete implementations of the plug-ins reference the interop library so that the classes in it can implement the required interfaces. The app has no direct knowledge of the plug-in library, and the plug-in library has no direct knowledge of the app. The can only potentially communicate through the interop interfaces.
In the plug-in scenario, it's common for the app to have a designated folder where plug-ins are located. At run-time, it scans the folder for assemblies, scans each assembly for classes/structs, examines each class/struct for known (interop) interfaces, and dynamically loads whatever matching plug-ins it finds by interface. Depending on the mechanism, each plug-in is usually added to a menu somewhere so that it can be used by the end user.
In this model, plug-ins can be updated without having to recompile the app, and the app can be updated without having to recompile the plug-ins. As long as the interop library version/signature doesn't change, the app itself or individual plug-ins can be updated/modified/fixed independently without having to redistribute the whole kit-n-kaboodle.
Also, 3rd parties that are going to develop plug-ins for your app only have to focus on implementing specific interfaces from your interop, without having to be concerned with the specifics of how your app consumes them.
Icognito 有一个很好的答案(我赞成)。我只想添加以下内容:
接口定义任何实现对象都必须具有的方法签名。它使您的代码能够调用这些对象的方法,而无需了解任何其他信息。
类定义方法签名,并且可以定义方法主体和属性。一个类可以实现一个接口。这使您能够将数据和操作代码保存在一起。
Icognito 的远程示例实际上比自行车更好。电视可能有这样的接口:
处理该接口的几个对象将是一个或多个电视对象和一个远程对象,例如:
在上面,索尼和东芝制造商可能都有自己的电视类层次结构。然而,它们都实现了通用的 ITelevision 接口,这使得针对这些类的编码变得更加容易。
另请注意,接口意味着实现由实现类决定。归根结底,只要每个人都实施 ITelevision,任何遥控器都可以控制任何电视。即使是未来的......
最后一点:抽象类与接口类似,抽象类需要后代提供方法体。但是,因为一个类只能从单个父类继承,而一个类可以实现任意多个接口。
Icognito has a good answer (which I upvoted). I only wanted to add the following:
An Interface defines method signatures that any implementing object must have. It enables your code to call methods on those objects without knowing anything else about it.
A class defines method signatures and may define method bodies and properties. A class may implement an Interface. This gives you the ability to keep both data and the manipulation code together.
Icognito's remote example was actually better than the bicycle. A TV might have an Interface like:
A couple of objects that might deal with that interface would be one or more TV objects and a Remote object like:
In the above, both the Sony and Toshiba manufacturer might have their own class hierarchy for the TV's. However, they both implement the common ITelevision interface which makes coding against those classes MUCH easier.
Note also that an interface means that implementation is left up to the implementing class. At the end of the day as long as everyone implements ITelevision the any remote will be able to control any TV. Even future ones...
A final note: Abstract classes are similar to Interfaces in that abstract classes require descendants to provide the method bodies. However, because a class may only inherit from a single parent class whereas a class may implement as many interfaces as you want.