電腦科學中,型別簽章(英語: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)

參考文獻 編輯

  1. ^ C++ Reference: Programming terms. [3 December 2013]. (原始內容存檔於2020-01-19). 
  2. ^ Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects). docs.oracle.com. [2020-05-19]. (原始內容存檔於2021-04-17).