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

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

问题描述

接口的原因真的让我难以理解.据我了解,这是一种解决 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,它也可以是抽象类,甚至是普通类(如果所有这些都可以派上用场)子类需要共享一些公共代码——然而,在大多数情况下,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天全站免登陆