這工作(見代碼語法註釋):
Type
TRec = record
s: string;
p: procedure; // As Ken pointed out, better define a procedural type:
// type TMyProc = procedure; and declare p : TMyProc;
end;
procedure run; forward; // The forward is needed here.
// If the procedure run was declared in the interface
// section of a unit, the forward directive should not be here.
Const
Rec: TRec = (s:''; p:run); // The const record is predefined by the compiler.
procedure run;
begin
WriteLn('Test');
end;
begin
Rec.p; // Rec.run will not work since run is not a declared member of TRec.
// The array index (Rec[0]) is not applicable here since Rec is not declared as an array.
ReadLn;
end.
雖然這是很好的,這不是什麼問。 (不是downvoting,順便說一下)。海報問他們是否可以將'procedure run'分配給'Rec.p',然後使用'Rec.run' - 你的代碼使用'Rec.p'顯示,這與所要求的不同。 – 2012-03-23 23:57:29
@KenWhite,你可能是對的,但我的例子會讓OP通過const記錄Rec調用運行過程。 – 2012-03-24 00:03:06
沒錯,但這並不是什麼問題,這是「可以稍後運行:Rec [0] .run;」,這是*不可*。 :)不管 - OP似乎認爲你的解決方案有效。 (刪除我的答案,並upvoting你的。) – 2012-03-24 00:12:08