如何使用记录定义和运行过程/函数



我想用过程或函数定义记录。你能帮我学语法吗?

Type TRec = record
 s: string;
 p: procedure;
end;
procedure run;
Const
  Rec: TRec = ('',run);
procedure run;
begin
end;

可以稍后运行:

Rec[0].run;

这很有效(请参阅代码中的语法注释):

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.

最新更新