请教高手(40分)

  • 主题发起人 主题发起人 poorness
  • 开始时间 开始时间
P

poorness

Unregistered / Unconfirmed
GUEST, unregistred user!
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type

AFunctionType = function(IntIn : integer) : integer;
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function AddProc(IntIn : integer) : integer;
function SubProc(IntIn : integer) : integer;
procedure PassAFunction(var IntIn : integer;
fn : AFunctionType);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}


function TForm1.AddProc(IntIn : integer) : integer;
begin
Result := IntIn + 1;
end;

function TForm1.SubProc(IntIn : integer) : integer;
begin
Result := IntIn - 2;
end;

procedure TForm1.PassAFunction(var IntIn : integer;
fn : AFunctionType);
begin
IntIn := fn(IntIn);
end;



procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;

begin
i := 10;

PassAFunction(i, @AddProc);
ShowMessage(IntToStr(i));

PassAFunction(i, @SubProc);
ShowMessage(IntToStr(i));
end;
运行提示:
在 PassAFunction(i, @AddProc);
variabled required
请问高手如何解决


end.
 
PassAFunction(i, @AddProc);
应改为PassAFunction(i, AddProc);
 
沒有人會嗎?help me
 

請高手說說把函數或者過程作為參數傳送該如何調用,
 
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type

AFunctionType = function(IntIn : integer) : integer of Object
//改这里
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
function AddProc(IntIn : integer) : integer;
function SubProc(IntIn : integer) : integer;
procedure PassAFunction(var IntIn : integer;
fn : AFunctionType);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}


function TForm1.AddProc(IntIn : integer) : integer;
begin
Result := IntIn + 1;
end;

function TForm1.SubProc(IntIn : integer) : integer;
begin
Result := IntIn - 2;
end;

procedure TForm1.PassAFunction(var IntIn : integer;
fn : AFunctionType);
begin
IntIn := fn(IntIn);
end;



procedure TForm1.Button1Click(Sender: TObject);
var
i : integer;

begin
i := 10;

PassAFunction(i, AddProc)
//改这里
ShowMessage(IntToStr(i));

PassAFunction(i, SubProc)
//改这里
ShowMessage(IntToStr(i));
end;
 
即:修改 AFunctionType 的的定义为:
AFunctionType = function(IntIn : integer) : integer of Object;

然后修改调用 PassAFunction 的地方为:
PassAFunction(i, AddProc);
...
PassAFunction(i, SubProc);
 
后退
顶部