接受不同数据类型参数的函数的正确术语是什么?
What is the correct term for a function that accepts its argument in different data types?
最近我一直在努力向我的同事解释某种方法的工作方式。问题与我不知道某个术语(可能存在)有关。采取这样的功能:
function myFunct (arg) {
if (typeof arg == "number") {
// ...
}
if (typeof arg == "string") {
// ...
}
}
根据 arg
的数据类型,该方法会做一些不同的事情。这种接受不同数据类型参数的函数的正确术语是什么?
这叫做"polymorphism",这里是wikipedia的定义:
... polymorphic functions that can be applied to arguments of different types, but that behave differently depending on the type of the argument to which they are applied (also known as function overloading or operator overloading)
在 C++ 等静态类型语言中,可以定义多个具有相同名称但不同参数的函数。例如 myFunct(int arg)
和 myFunct(string arg)
.
在动态类型语言中,例如 php 或 python,该函数可以接受任何类型的参数,并根据示例中的类型做不同的事情。
最近我一直在努力向我的同事解释某种方法的工作方式。问题与我不知道某个术语(可能存在)有关。采取这样的功能:
function myFunct (arg) {
if (typeof arg == "number") {
// ...
}
if (typeof arg == "string") {
// ...
}
}
根据 arg
的数据类型,该方法会做一些不同的事情。这种接受不同数据类型参数的函数的正确术语是什么?
这叫做"polymorphism",这里是wikipedia的定义:
... polymorphic functions that can be applied to arguments of different types, but that behave differently depending on the type of the argument to which they are applied (also known as function overloading or operator overloading)
在 C++ 等静态类型语言中,可以定义多个具有相同名称但不同参数的函数。例如 myFunct(int arg)
和 myFunct(string arg)
.
在动态类型语言中,例如 php 或 python,该函数可以接受任何类型的参数,并根据示例中的类型做不同的事情。