界面只是“语法糖”吗? [英] Are Interfaces just "Syntactic Sugar"?

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

问题描述

我一直在玩PHP和Python。

I've been playing mostly with PHP and Python.

我一直在阅读OO编程中的接口,并且看不到使用它的优势。

I've been reading about Interfaces in OO programming and can't see an advantage in using it.

多个对象可以实现相同的接口,但多重继承也不能提供这个功能吗?

Multiple objects can implement the same interface, but multiple inheritance doesn't provide this as well?

为什么我是否需要创建一个没有实现的接口 - 主要是合同 - 如果我可以检查Python中的对象中是否存在一个方法,该方法是从多个类继承的吗?

Why do I need to create an Interface "with no implementation" - mainly a "contract" - if I can just check if a method exists in an object in Python, that inherits from multiple classes?

接口是用其他语言创建的,因为它们不提供多重继承?或者我在这里错过了一些更重要的东西?

Do Interfaces were created in another languages because they don't provide multiple inheritance? Or am I missing something more important here?

推荐答案

接口的用处与静态类型的有用性直接相关。如果您使用的是动态类型的语言(如PHP或Python),那么界面确实不会显着增加语言的表现力。也就是说,任何可以被描述为使用接口的程序都可以在没有使用接口的情况下表达而没有显着差异。

The usefulness of an interface is directly connected to the usefulness of static typing. If you're working in a dynamically-typed language like PHP or Python, interfaces truly don't add significantly to the expressiveness of the language. That is, any program that can be described as using interfaces can be expressed without significant difference without using interfaces.

因此,Python有一个相当模糊的概念 protocol(符合某种模式的实现,如迭代协议)基本上是相同的东西,但没有编译时检查的其他好处是有限的。

As a result, Python has a fairly nebulous concept of a "protocol" (an implementation conforming to a certain pattern, like the iteration protocol) which amounts to essentially the same thing, but without the other benefits of compile-time checking its value is limited.

在静态类型的语言中,在另一方面,接口对于允许实现与实现分离是必不可少的。在静态语言中,必须在编译时解析所有表达式的类型,因此通常必须在此时绑定到实现,从而限制运行时的灵活性。接口定义如何访问功能而不定义特定的实现,这允许静态语言证明表达式是正确的而无需访问实现。

In a statically-typed language, on the other hand, an interface is essential to allow implementation to be decoupled from implementation. In a static language, the types of all expressions must be resolved at compile time, so normally bindings to implementation must be made at that time, limiting run-time flexibility. An interface defines how to access functionality without defining a specific implementation, which allows a static language to prove that expressions are correct without having access to the implementation.

如果没有接口(或像C ++的纯虚函数那样的等效公式),静态类型语言的表现力将受到严重阻碍。实际上,存在许多实现(Win32和COM立即想到)通过在结构中存储函数指针(从而重新实现C ++的虚函数和手动vtable调用)来实质上重现C中的接口和虚拟调度的大部分功能。 。在这种情况下,表现力存在很大差异,因为程序中需要进行许多更改才能表达相同的概念。

Without interfaces (or an equivalent formulation like C++'s pure virtual functions), the expressiveness of a statically-typed language would be severely hampered. In fact, many implementations exist (Win32 and COM come immediately to mind) to essentially reproduce much of the functionality of interfaces and virtual dispatch in C by storing function pointers in structures (and thus re-implementing C++'s virtual functions and vtable invocation by hand). In this case there is a big difference in expressiveness, since many changes are required in the program to express the same concepts.

接口只是类型多态的一个例子,在那个相当有限的一个。在支持参数多态性(又名 generics )的语言中,您可以完成更多。 (例如,没有通用接口,C#的 LINQ 将无法实现。)对于更强大的同类型的形式,请查看Haskell的类型类

Interfaces are just one example of type polymorphism, and a fairly limited one at that. In languages that support parametric polymorphism (aka generics) you can accomplish much more. (For example, C#'s LINQ would not be possible without generic interfaces.) For a much more powerful form of the same kind of thing, look into Haskell's typeclasses.

这篇关于界面只是“语法糖”吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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