电脑科学中,类型签名(英语:type signature)或类型注解type annotation)是对程序的函数、方法、子过程、以及变量等给出其类型。特别是对函数给出其输入参数数量、类型与次序及输出结果的类型。 许多编译器产生的内部使用的函数名包含了其类型特征,这称为名字修饰,为链接器辨别不同的函数提供了方便。[1]

类型特征的现代应用:

  • 面向对象语言使用的interface,实际上是利用了函数类型特征的模板。
  • C++支持的函数重载实际上用不同的类型特征来辨识。
  • 多继承要求考虑函数特征,以避免不可预计的结果。

C/C++

CC++ 中,类型签名通常被声明为函数原型,函数声明也反映了其用法。例如,函数指针使用方法如下:

char c;
double d;
int retVal = (*fPtr)(c, d);

它的签名为:

(int) (char, double);

Java

在Java中,方法签名(英语:method signature)由方法的名字(method's name)与参数类型(parameter types)组成。[2] 如下例:

public double calculateAnswer(double wingSpan, int numberOfEngines,
                              double length, double grossTons) {
    //do the calculation here
}

该例的方法签名为:calculateAnswer(double, int, double, double)

参考文献

Wikiwand in your browser!

Seamless Wikipedia browsing. On steroids.

Every time you click a link to Wikipedia, Wiktionary or Wikiquote in your browser's search results, it will show the modern Wikiwand interface.

Wikiwand extension is a five stars, simple, with minimum permission required to keep your browsing private, safe and transparent.