什么是“实现接口”?到底是什么意思 [英] What does "implementing an interface" exactly mean?

查看:581
本文介绍了什么是“实现接口”?到底是什么意思的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近几天,我经常遇到实现接口一词……我对它有什么了解,但我想了解更多信息和一些资源。类何时实现接口?

Last days I meet the term "implementing an interface" really often...I have an idea what is it about but I would like more information and some resources about it. When does a class implement an interface?

推荐答案

实现接口是什么意思?

也许最好是从接口是什么开始,然后着手实现一种实现的意思。

Perhaps it's best if we start with what an interface is, and then work towards what implementing one means.

示例1:

世界上每一个麦当劳都有某些共同点。实际上,如果您想经营麦当劳的专营权,您 必须 遵循其加盟商规则:

Every single McDonald's in the world has certain commonalities. In fact, if you want to run a McDonald's franchise, you must follow their franchisee rules:


  1. 必须出售巨无霸。

  2. 餐馆必须有Big M

  3. 必须支付加盟费。

  4. 餐馆必须始终保持整洁(很顺口!?)。

  1. Must sell Big Macs.
  2. Restaurants must have a Big M
  3. Must pay franchisee fees.
  4. Restaurants must be clean (sic!?) at all times.

这些是他们的规定。在合同中。每家餐厅都必须遵守该合同。每个餐厅略有不同,但是在上述规则上它们是相同的。

These are their rules. It's in the contract. Every restaurant is bound to follow that contract. Each restaurant is slightly different, but they are the same when it comes to the above rules.

对于客户来说,这也是一件非常好的事情。如果您走进世界上任何一家麦当劳餐厅,就可以肯定会买到巨无霸。

This is also a very good thing for a customer. If you walk into any McDonald's restaurant in the world, you know for certain that you can purchase a Big Mac.

麦当劳与界面有什么关系?

接口不过是合同。任何实施或签署合同的餐厅都必须遵守。或者换句话说,任何实现麦当劳加盟商界面的餐厅都必须遵循它。

An "interface" is nothing more than a contract. Any restaurant that "implements" or signs the contract, is bound to follow it. Or in other words, any restaurant that implements the McDonald's franchisee interface, is bound to follow it.

为什么将它称为IMcDonald界面而不是McDonald界面?

每次命名接口时,通常都以 I开头。

When ever you name an interface, it is common practice for the name to begin with an "I".

示例#2

世界上所有飞机,无论哪种类型,都具有某些共同点。换句话说,它们都实现了Iplane接口。

All planes in the world have certain commonalities, no matter what type. In other words, they all implement the Iplane interface.

IPlane接口规定,实现该接口的任何平面都必须具有:

The IPlane interface stipulates that any plane which implements it must have:


  1. 两个机翼

  2. 一个引擎

  3. 它必须飞行

因此,如果一架波音737遵循这些规则,作为客户,您可以保证购买的飞机会带有机翼并能飞翔。这是实现上述接口的波音737的示例:

Therefore, if a Boeing 737 follows these rules, as a customer, you a guaranteed that your purchase will have wings and will fly. Here is an example of a Boeing 737 implementing the above interface:

 public interface IPlane
{
    void Fly();
    void HasTWoWings();
    void Engine();
}

class Boeing737 : IPlane //  <-------------- the Boeing 737 implements the interface
{
    // this means that the Boeing737 MUST have a fly, hastwowings and an engine method.
    // the interface doesn't specify HOW the plane must fly. So long as it does fly
    // the compiler doesn't care.


    public void Fly()
    {
        Console.WriteLine("Come fly with me, let's fly, let's fly awaaaaaaaay");
    }

    public void HasTWoWings()
    {
        Console.WriteLine("I've got two wings");

    }

    public void Engine()
    {
        Console.WriteLine("BRrrrrrrrooooooooooooooooooooooooooooooooom!");
    }
}

我希望对您有所帮助。

那么,这有什么用?

如果您是乘客,这将非常方便例如想要预订飞机。您不确定会为您提供哪些飞机。而且,只要它们是飞机,只要它们飞行并且有两个机翼和一个引擎,您就不必在意。当您在机场时,您可能会乘坐波音飞机,或者可能会乘坐空中客车,或者可能会乘坐Stig飞机-但是您知道,无论您乘坐的是哪种实际飞机,他们都可以完成某些工作。它们可能以不同的方式飞行,但是它们都可以飞行。

This becomes very handy if you're a passenger wanting to book a plane for example. You're not sure what planes will be available for you. And you don't exactly care, so long as they are planes, and so long as they fly, and have two wings and an engine. When you're at the airport, you might get a Boeing, or you might get an airbus, or you might get a Stig - but you know that no matter what actual plane you get, that they will do a certain job. They might fly in different ways, but they all fly.

这可以让您以减少维护成本并显着简化代码的方式编写代码进行修改而不会导致错误。

This allows you to write your code in such a way so as to minimise maintenance costs and to make it significantly easier to modify without causing bugs.

这篇关于什么是“实现接口”?到底是什么意思的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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