什么其他语言允许程序员使用Interfaces作为功能参数? [英] What other languages allow programmers to use Interfaces as function parameters?

查看:144
本文介绍了什么其他语言允许程序员使用Interfaces作为功能参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我知道,在Java中 ,你可以这样做:

  int myMethod(Burnable obj){

/ *只有在参数实现Burnable
*界面
* /
}

我喜欢在 PHP 最多,我不确定是否可以在PHP中执行此操作



此外,我想了解其他相当主流的语言,这在我看来,它是将模块化构建到您的代码中。



谢谢

解决方案

所有静态类型的语言(C,C ++,Java,Objective-C,Ada等)其他)允许一个指定函数参数的类型。它们的一个子集允许重载(具有相同名称但具有不同类型参数的多个不同功能)。自PHP5起,可以使用输入提示来指定参数类型验证该函数调用预期类型的​​传递参数;然而,PHP不支持重载(尽管您可以使函数接受多种不同的类型,然后根据类型检查类型并分派到不同的实现。)


Let me explain:

I know that, in Java, you can do stuff like this:

int myMethod(Burnable obj){

 /*do stuff that's only applicable if the argument implements the Burnable 
  *interface
  */
}

I like programming in PHP the most, and I'm not sure whether I can do that in PHP too.

Furthermore, I'd like to know what other reasonably mainstream languages that feature this, as in my view it's a way of building modularity into your code.

Thank you

解决方案

All statically-typed languages (C, C++, Java, Objective-C, Ada, and many others) allow one to specify the types of a function's parameters. A subset of them allow for overloading (having multiple different functions with the same name but with different typed parameters). Since PHP5, it is possible to specify the types of parameters using type hinting to validate that function calls pass parameters of the expected type; however, PHP does not support overloading (although you can make functions accept multiple different types, and then check the types and dispatch to different implementations by type).

这篇关于什么其他语言允许程序员使用Interfaces作为功能参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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