需要界面 [英] need of interface

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

问题描述

由于我们必须重新定义接口中预先声明的所有方法,那么为什么还要创建接口并在其他类中实现呢?

解决方案

在构造类之前,可以选择我们计划在其中实现的功能.接下来,我们可以通过两种方式进行操作,即使用抽象类或创建接口.

在我的最后一个非常简单的应用程序中,我首先创建了3个接口(IReader,IWriter,IAuthenticate),然后创建了可以访问数据的类.事物支持类实例的排序.您想使用我的控件.我什至不知道如何分类您的特定班级?

我可以让您从我自己的抽象类中继承-但这没有任何意义,因为这意味着我提供的基本实现可以根据需要重写.由于我不了解您的课程,因此没有真正的方法可以提供任何有用的基础实现.另外,您将无法从任何其他类派生(C#中没有多重继承).

另外,我可以为您提供一个界面,并让所有实现留给您.因为我知道您将有一个Sort方法,所以即使您不知道您的类,也可以从我的代码中调用它!我所有控件需要做的就是拥有一个采用该接口参数的方法,而我们每个人都可以得到您想要的结果.


接口是一个契约.它告诉其他人您的类支持某些方法-它没有说出它们是如何实现的,只有那些方法存在.实际上,这意味着您可以通过许多不同的方式来完成同一件事.如果您想使用控制反转(IoC)之类的工具,则绝对无价.

例如,如果要在C#中使用using模式,则您的类必须实现IDisposable.在幕后,编译器将using块转换为以下内容:

SomeClass someClass = null;
try
{
  someClass = new SomeClass();
}
finally
{
  ((IDisposable)someClass).Dispose();
}

您会看到someClass被强制转换为IDisposable ,这允许应用程序调用Dispose.


Since we have to redefine all the methods predeclared in the interface then why we need to create interface and implement then in other classes?

解决方案

Before constructing classes we can choose those features we plan to implement inside them. Next we can act in two ways which are to use abstract class or to create interface.

In my last application that was quite simple first i created 3 interfaces (IReader, IWriter, IAuthenticate) and then created classes to have access to data.


Lets say I develop a control that among other things supports sorting of class instances. You want to use my control. How am I supposed to know how to sort your particular class when I don''t know it even exists?

I could make you inherit from an abstract class of my own - but that makes no sense as it would imply that I am providing a base implementation that you can override if desired. As I have no knowledge of your class there is no real way to provide a base implementation that would be of any use. Also, you would not be able to derive from any other classes (no multiple inheritance in C#).

Alternatively I can provide you with an interface and leave all the implementation up to you. Because I know you will have a Sort method, I can call it from my code without even knowing about your class! All my control needs to do is have a method that takes a parameter of that interface and we all have the result you desire.


An interface is a contract. It tells others that your class supports certain methods - it doesn''t say how they are implemented, only that those methods are present. What this means, in practice, is that you can accomplish the same thing in many different ways; absolutely invaluable if you want to use something like Inversion of Control (IoC).

As an example, if you want to use the using pattern in C# then your class must implement IDisposable. Behind the scenes, the compiler translates the using block into the following:

SomeClass someClass = null;
try
{
  someClass = new SomeClass();
}
finally
{
  ((IDisposable)someClass).Dispose();
}

There you see someClass being cast to IDisposable which allows the application to call Dispose.


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

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