什么是接口,为什么我们需要它 [英] what is Interface why do we need It

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

问题描述


希望我不要浪费您宝贵的时间,我在.net拥有4.4年的经验,
我是Hp的员工,现在我正在寻找工作变动,今天我参加了面试
在班加罗尔,面试官只问了一个问题,那就是界面,
我不是说像用于多重继承那样,那个家伙也没有期望得到这个答案.
我说了些什么.

那只有一个问题,后来他说好.

技术人员,请告诉我为什么我们要使用界面.给我一个样品.

我从来没有实现过.

如果我的问题很基本,请忽略.

Hi,
Hope im not wasting your valueble time,i have 4.4 years of experience in .net,
im employee of Hp, right now im looking for jobchange,today i was attend the interview
in bangalore,the interviewer asked only one question that is what is interface,
i was not said like used for multiple inheritance, and that guy also not expected that answer.
i told some thing.

that only one question , later he said ok.

techies, kindly tell me why we go for interface . give me a sample.

i never implemented it.

if my question was very basic ,kindly ignore.

推荐答案

接口是一种契约:当您向类中添加接口时,即表示您同意遵守以下规则:界面放下,作为回报,您的班级可以加入专属会员俱乐部,并从该会员资格中获得好处,无论他们是什么人.

例如,所有数组和通用集合对象(例如List< T>)都实现IEnumerable,因此它们都可以在foreach循环中使用-如果您的类实现了IEnumerable接口,则可以在也是foreach.

我用合理的量.最近,我需要一个面板,其中包含控件,控件会自动调整大小,具体取决于哪些控件已展开和哪些控件已缩小.因此,我创建了一个FlexPanel来容纳它们,并创建了一个IFlexPanel接口,控件必须实现该接口:

An interface is a contract: when you add an Interface to your class you are agreeing to abide by the rules that the interface lays down, and in return it allow your class to join the exclusive membership club, and get the benefits of that membership, no matter what they might be.

For example, all arrays, and Generic Collection objects (such as List<T>) implement IEnumerable, and as a result they are all usable in a foreach loop - if your class implements the IEnumerable Interface, then it can be used in foreach as well.

I use them a reasonable amount. Recently, I needed a panel which contained controls which it auto sized, depending on which were Expanded and which were Shrunken. So I created a FlexPanel to hold them, and an IFlexPanel interface which the controls had to implement:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UtilityControls
    {
    /// <summary>
    /// Interface to add support for FlexControls within a FlexPanel
    /// </summary>
    /// <remarks>
    /// When you implement this interface:
    ///
    ///    DefaultShrunken
    /// Indicates the desired initial state of your control. You may or may not get
    /// your wish, depending on the AutoSize mode of the FlexPanel, and the DefaultShrunken
    /// status of other controls, combined with your controls position in the list
    /// of FlexControls
    ///
    ///    Shrinking
    ///    Expanding
    /// Are called when the appropriate actions are being taken - this allows you to
    /// update buttons, or remove toolbars, etc. to reflect the visual state in your
    /// control.
    ///
    ///    RequestShrink
    ///    RequestExpand
    ///    RequestToggleSize
    /// Are Events that the FlexPanel handles to change the size of your control
    /// Note that your panel may change size without you signalling any of these
    /// if the FlexPanel is set to AutoSize mode and a different panel is Expanded
    /// or Shrunk.
    /// </remarks>
    public interface IFlexControl
        {
        /// <summary>
        /// Called when the the control is to be shrunk to mininum height
        /// </summary>
        void Shrinking();
        /// <summary>
        /// Called when the control is to be enlarged
        /// </summary>
        void Expanding();
        /// <summary>
        /// User request to shrink this control
        /// </summary>
        /// <remarks>
        /// The parent FlexPanel will handle the actual decision to shrink.
        /// </remarks>
        event EventHandler RequestShrink;
        /// <summary>
        /// User request to enlarge this control
        /// </summary>
        /// <remarks>
        /// The parent FlexPanel will handle the actual decision to enlarge.
        /// </remarks>
        event EventHandler RequestExpand;
        /// <summary>
        /// User request to change the Shrunk / Enlarged state of this control
        /// </summary>
        /// <remarks>
        /// The parent FlexPanel will handle the actual decision to enlarge or shrink.
        /// </remarks>
        event EventHandler RequestToggleSize;
        /// <summary>
        /// If true, will default to shruken when first shown.
        /// </summary>
        bool DefaultShrunken { get; set; }
        }
    }

提供了控件,可以实现两种方法收缩"和扩展",三个事件"RequestShrink","RequestExpand"和"RequestToggleSize"以及属性"DefaultShrunken",它们可以基于任何Control或UserControl以及它们将根据需要自动调整大小.

在这里使用接口意味着我以后不必再使用基于Button或DataGridView甚至UserControl的控件了,任何实现IFlexControl的东西都可以.记住,C#仅允许您从单个类派生您的类-因此,如果我将IFlexControl构造为普通类或抽象类,则仅将从该类派生的控件装入FlexPanel.


[edit]代码块已从"xml"更改为"c#"-发布前我没有注意到-OriginalGriff [/edit]

Provided the control implemented the two methods "Shrinking" and "Expanding", the three events "RequestShrink", "RequestExpand" and "RequestToggleSize" and the property "DefaultShrunken" they could be based on any Control or UserControl and they would be autosized as necessary.

Using an interface here means that I don''t have to restrict myself later to having to use controls based on a Button, or a DataGridView, or even a UserControl - anything which implements IFlexControl will be fine. Remember, C# only allows you to derive your class from a single class - so if I constructed IFlexControl as a normal or abstract class then only controls derived from that class coule be loaded into a FlexPanel.


[edit]Code block changed from "xml" to "c#" - I didn''t notice before I posted - OriginalGriff[/edit]


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

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