接口-有什么意义? [英] Interfaces — What's the point?

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

问题描述

界面的原因确实使我难以理解。据我了解,这是针对C#中不存在的不存在的多重继承的一种解决方法(或者有人告诉我)。

The reason for interfaces truly eludes me. From what I understand, it is kind of a work around for the non-existent multi-inheritance which doesn't exist in C# (or so I was told).

我所看到的是,您预先定义了一些成员和函数,然后必须在类中重新定义它们。从而使接口冗余。只是感觉像语法...好吧,对我而言是垃圾(请不要冒犯,指的是垃圾,就像无用的东西一样)。

All I see is, you predefine some members and functions, which then have to be re-defined in the class again. Thus making the interface redundant. It just feels like syntactic… well, junk to me (Please no offense meant. Junk as in useless stuff).

在下面给出的示例中,取自不同的C#接口线程上的堆栈溢出,我只创建一个名为Pizza的基类而不是接口。

In the example given below taken from a different C# interfaces thread on stack overflow, I would just create a base class called Pizza instead of an interface.

简单示例(取自不同的堆栈溢出贡献)

easy example (taken from a different stack overflow contribution)

public interface IPizza
{
    public void Order();
}

public class PepperoniPizza : IPizza
{
    public void Order()
    {
        //Order Pepperoni pizza
    }
}

public class HawaiiPizza : IPizza
{
    public void Order()
    {
        //Order HawaiiPizza
    }
}


推荐答案

重点是界面代表合同。任何实现类都必须具有的一组公共方法。从技术上讲,该接口仅控制语法,即那里有什么方法,它们得到什么参数以及它们返回什么。通常,它们也封装语义,尽管仅通过文档即可。

The point is that the interface represents a contract. A set of public methods any implementing class has to have. Technically, the interface only governs syntax, i.e. what methods are there, what arguments they get and what they return. Usually they encapsulate semantics as well, although that only by documentation.

然后,您可以具有接口的不同实现,并可以随意替换它们。在您的示例中,由于每个披萨实例都是一个 IPizza ,因此您可以在处理未知披萨实例的任何地方使用 IPizza 类型。任何类型从 IPizza 继承的实例都可以被订购,因为它具有 Order()方法。

You can then have different implementations of an interface and swap them out at will. In your example, since every pizza instance is an IPizza you can use IPizza wherever you handle an instance of an unknown pizza type. Any instance whose type inherits from IPizza is guaranteed to be orderable, as it has an Order() method.

Python不是静态类型的,因此类型会保留并在运行时查找。因此,您可以尝试在任何对象上调用 Order()方法。只要对象具有这样的方法,运行时就会很高兴,并且可能只是耸耸肩,说 Meh。(如果没有)。在C#中不是这样。编译器负责进行正确的调用,如果它只有一些随机的 object ,则编译器尚不知道运行时实例是否具有该方法。从编译器的角度来看,它无效,因为它无法验证它。 (您可以使用反射或 dynamic 关键字来执行此类操作,但是我想这有点远了。)

Python is not statically-typed, therefore types are kept and looked up at runtime. So you can try calling an Order() method on any object. The runtime is happy as long as the object has such a method and probably just shrugs and says »Meh.« if it doesn't. Not so in C#. The compiler is responsible for making the correct calls and if it just has some random object the compiler doesn't know yet whether the instance during runtime will have that method. From the compiler's point of view it's invalid since it cannot verify it. (You can do such things with reflection or the dynamic keyword, but that's going a bit far right now, I guess.)

还请注意,通常意义上的接口不一定必须是C# interface ,它也可以是抽象类,甚至可以是普通类(如果所有子类都需要共享一些通用代码,这会派上用场-但是,在大多数情况下,接口就足够了。)

Also note that an interface in the usual sense does not necessarily have to be a C# interface, it could be an abstract class as well or even a normal class (which can come in handy if all subclasses need to share some common code – in most cases, however, interface suffices).

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

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