为什么我们使用界面 [英] Why we use interface

查看:124
本文介绍了为什么我们使用界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

/// Interface demo
Interface Iinterface1
{
// Function prototype
public void ShowDetails();
}

// First class using the interface
Class MyClass1 : Iinterface1
{
public void ShowDetails()
{
// Function body comes here
Response.Write("I'm in MyClass");
}
}

// Second class using the interface
Class MyClass2 : Iinterface1
{
public void ShowDetails()
{
// Function body comes here
Response.Write("I'm in MyClass2");
Response.Write("So, what?");
}
}


上面的示例说明了界面.但是,可以通过直接在MyClass2中创建方法来实现上述目的.我认为这会造成混乱.每个面试官都询问Interface.

那为什么我们要使用界面呢?


Above example explained the interface. But above can be achieved by directly create method in MyClass2. I think it creates confusion. Every interviewer asking about Interface.

Then why we use interface?
Can any one give good example?

推荐答案

基本上,接口是一种在C#中提供某种多重继承的方法.
因为您只能从单个基类派生一个类(否则事情会变得很混乱),因此如果没有它,很难提供其他功能.基类描述类 的作用-接口描述类 可以做什么 .

他们提供了一项合同:如果您实现了所需的功能,则可以将我们视为我们中的一员".例如,如果您的Student类是从People派生的,并且实现IEnumerable,则它具有People的所有功能,但也可以在foreach循环中使用.

在类实现相同接口的地方,尽管它们是不相关的类,但它们以相同的方式和通过相同的方法集调用全部.
Basically, interfaces are a way to provide some kind of multiple inheritance in C#.

Because you can only derive a class from a single base class (or things start to get very confusing) it is difficult to provide additional features without it. A Base class describes what a class is - Interfaces describe what a class can do.

They provide a contract: "If you implement the required bits, you can be treated as one of us". For example, if your Students class is derived from People, and implements IEnumerable, then it has all the features of People, but it can also be used in a foreach loop.

Where classes implement the same interface, they call all be used in the same way, and through the same method set, despite being unrelated classes otherwise.


除了其他答案,我还想解释通常被忽略的一件事.

接口可以通过值类型实现吗?是的,按结构.

这是一个非常重要的事实.它允许多态( http://en.wikipedia.org /wiki/Polymorphism_%28computer_science%29 [ ^ ]),其中多态容器是如此抽象的对象(由某些接口表示),因此即使知道实现类型的本质,无论它们是否是类,该容器仍是不可知的.

此外,请查看格里夫(Griff)的回答和我对此的评论.由于多重继承的弱形式,表示接口的类型可以参与不止一种需要不同接口类型的多态关系.

—SA
In addition to other answers, I want to explain one thing which is usually overlooked.

Can interface be implemented by value types? Yes, by structures.

This is a very important fact. It allows for much more flexible form of polymorphism (http://en.wikipedia.org/wiki/Polymorphism_%28computer_science%29[^]), where the polymorphous containers operate so abstract objects (represented by some interface(s)), so the container remains agnostic even to the knowledge of the nature of implementing type, are they classes or not.

Besides, look at the answer by Griff and my comment to it. The types representing be the interfaces can participate in more than one polymorphous relationship requiring different interface types due to weak form of multiple inheritance.

—SA


正如曼弗雷德(Manfred)使用基于接口的设计概念所提到的那样,它提供了松耦合,基于组件的程序设计 g,更易于维护 y,使您的代码库更具可扩展性,并且使代码重用更加易于访问,因为实现与接口分离.

界面在您的应用程序中添加了 plug play 架构.接口可帮助定义应用程序和其他对象之间的合同(协议或蓝图,但是您选择定义它).这表明对象公开了哪种方法,属性和事件.因此,我们可以说一个接口是一个契约,它定义了某些功能的签名.

希望对您有所帮助:)
As Manfred mentioned using interface based design concept 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. Interfaces help define a contract (agreement or blueprint, however you chose to define it), between your application and other objects. This indicates what sort of methods, properties and events are exposed by an object. Hence we can say that an interface is a contract that defines the signature of some piece of functionality.

hope it helps :)


这篇关于为什么我们使用界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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