type
TMyFunction = function(n: Integer): string;
type
myMethodDelegate = function(myInt: Integer): string of object;
//-----------------------------------------------------------------
TmySampleClass = class
public
function myStringMethod (myInt: Integer): string;
class function mySignMethod (myInt: Integer): string; static;
end;
{ mySampleClass }
class function TmySampleClass.mySignMethod
(myInt: Integer): string;
begin
Result := '';
if (myInt > 0) then Result := '+';
if (myInt < 0) then Result := '-';
end;
function TmySampleClass.myStringMethod
(myInt: Integer): string;
begin
Result := 'zero';
if (myInt > 0) then Result := 'positive';
if (myInt < 0) then Result := 'negative';
end;
procedure TForm1.W3Button7Click(Sender: TObject);
var
mySC: TmySampleClass;
x: myMethodDelegate;
y: myMethodDelegate;
IntegerToString : TMyFunction;
begin
IntegerToString := IntToStr;
WriteLn(IntegerToString(12345));
mySC := TmySampleClass.Create;
try
WriteLn(Format('%d is %d; use the sign %d',[5, mySC.myStringMethod(5), mySC.mySignMethod(5)]) );
WriteLn(Format('%d is %d; use the sign %d',[-3, mySC.myStringMethod(-3), mySC.mySignMethod(-3)]) );
WriteLn(Format('%d is %d; use the sign %d',[0, mySC.myStringMethod(0), mySC.mySignMethod(0)]) );
WriteLn(sLineBreak);
{ Using Delegates }
x := mySC.myStringMethod;
y := mySC.mySignMethod;
WriteLn(Format('%d is %d; use the sign %d',[5, x(5), y(5) ]) );
WriteLn(Format('%d is %d; use the sign %d',[-3, x(-3), y(-3)]) );
WriteLn(Format('%d is %d; use the sign %d',[0, x(0), y(0) ]) );
finally
mySC.Free;
end;
end;
|