在C#中使用接口的需求是什么 [英] What is the need of use interface in C#

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

问题描述

在c#中需要使用界面是什么



我尝试了什么:



在c#

what is the need of use interface in c#

What I have tried:

what is the need of use interface in c#

推荐答案

中需要使用的接口是什么?接口允许您在不更改的情况下交换特定的实现您的应用程序的整体结构。



其细节可能完全不同,但与调用代码保持不变,如 Save()可以是具有不同实现的文件保存或保管箱保存等。
Interfaces allow you to swap out specific implementations without changing the overall structure of your application.

The details of which may be radically different but to the calling code it stays the same, like Save() which could be a file save or dropbox save etc. which have different implementations.


接口提供了灵活性,我们可以定义一个可以实现多个接口的实现。当你想从基类继承实现时,使用接口是一个不错的选择

简而言之,它提供松散耦合,基于组件的编程,更易于维护,使代码库更具可伸缩性并使代码成为可能重用更容易访问,因为实现与界面分离,接口在你的应用程序中添加一个插件和游戏结构。



让我们举一些车辆的例子,其中一些是4轮车,有些是两个,但常见的是每个车轮都是圆形的,所有都在移动所以这些类似的属性我们可以保持在一个界面,并可以继承它们4轮车和2轮车。
Interfaces provides flexibility, we can define a single implementation that can implement multiple interfaces. when you want to inherit implementation from a base class it is a good choice to use Interface
In short it provides loose coupling, component-based programming, easier maintainability, makes your code base more scalable and makes code reuse much more accessible because implementation is separated from the interface, Interfaces add a plug and play like architecture into your applications.

Lets take example of vehicles, in that some are 4 wheeler, some are two but the common things is that each vehicles wheel are round shape, all are moving so these similar property we can keep in an interface and can inherit them for 4 wheeler and 2 wheeler also.


不要忘记C#不支持多重继承,所以这是通过接口实现的。



C# - 继承 [ ^ ]



这是我的主题一分钱。
Don't forget that C# doesn't support multiple inheritance, so this is implemented with interfaces.

C# - Inheritance[^]

That was my one cent to the topic.


这篇关于在C#中使用接口的需求是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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