为什么我们不需要动态语言的接口? [英] Why don't we require interfaces in dynamic languages?

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

问题描述

仅仅是因为动态类型我们在python中不需要接口的概念(比如在Java和C#中)吗?

Is it just because of dynamic typing we don't require a concept of interfaces(like in Java and C#) in python?

推荐答案

作为关键字和工件的接口由Java 1 引入(并且C#从那里获取)来描述合同的内容对象必须遵守。

The interface as a keyword and artifact was introduced by Java1 ( and C# took it from there ) to describe what the contract an object must adhere was.

但是,接口一直是面向对象范式的关键部分,基本上它代表了一个对象必须响应的方法。 Java只是强制执行这种机制来提供静态类型检查。

But, interface has always been a key part of Object Oriented Paradigm and basically it represents the methods an object has to respond. Java just enforces this mechanism to provide statically type checking.

因此,动态(OO)编程语言使用接口,甚至认为他们不会静态检查它们。就像其他数据类型一样,例如在Ruby中:

So, dynamic ( OO ) programming languages do use interfaces, even thought they don't statically check them. Just like other data types, for instance in Ruby:

 @i = 1;

您无需声明 i 类型 FixNum 您只需使用它。接口也是如此,它们只是流动。权衡是,您无法对其进行静态检查,故障仅在运行时显示。

You don't have to declare i of type FixNum you just use it. Same goes for interfaces, they just flow. The trade-off is, you can't have a static check on that and failures are only show at runtime.

另一方面结构类型(或称为静态鸭子类型,我称之为:P)由Go或Scala等语言使用,提供了两全其美的效果。

In the other hand Structural type ( or static duck type as I call it :P ) used by languages as Go or Scala, gives the best of both worlds.


1.请参阅Daniel Earwicker关于CORBA的评论 interface keyword

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

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