2013-05-13 261 views
5

如何在Delphi中做一個程序的前置聲明並在其他地方執行?我想做一些像C這樣的代碼,但在Delphi中:delphi程序的前置聲明

void FooBar(); 

void FooBar() 
{ 
    // Do something 
} 
+2

爲什麼不在文檔中查找前向聲明? – 2013-05-13 18:35:40

+1

@DavidHeffernan:很有可能是因爲如果他知道要尋找什麼,他不必去查找它。看起來他的經驗與C,沒有一個「前進」的關鍵字或類似的東西。 – 2013-05-13 18:49:56

+2

@MasonWheeler再次閱讀問題的主題。在Delphi文檔搜索中輸入它。這是發生了什麼:http://docwiki.embarcadero.com/Search/?cx=0017879905796164350846%3Ad3x3zsyivu0&cof=FORID%3A9&ie=UTF-8&lr=lang_en&q=forward+declaration+of+procedure+in+delphi&sa=Go&siteurl=docwiki。 embarcadero.com%2FRADStudio%2FXE4%2Fen%2FMain_Page&ref = docs.embarcadero.com%2Fproducts%2Frad_studio%2F&ss = 160j25600j2我們應該鼓勵人們使用文檔。您的回答應包括對文檔的參考。 – 2013-05-13 18:56:19

回答

16

你這樣做,與forward指示,例如:

procedure FooBar(); forward; 

... 
//later on 

procedure FooBar() 
begin 
    // Do something 
end; 

這僅僅是必要的,如果你宣佈它作爲一個內部功能。 (即已經在您的設備的implementation部分內)。任何聲明爲類的方法或單元的interface部分的內容自動被理解爲是前向聲明的。

+0

謝謝,這工作。 – Seatless 2013-05-13 18:39:40

5

這是通過單元的接口/實現部分來完成它的一種方法。

Unit YourUnit; 

Interface 

    procedure FooBar(); // procedure declaration 


Implementation 

// Here you can reference the procedure FooBar() 

procedure FooBar(); 
begin 
    // Implement your procedure here 
end; 

你也應該看看到文檔中關於forward declarations,在另一個選項中提到,像@MasonWheeler回答。