Einzelnen Beitrag anzeigen

SProske

Registriert seit: 16. Feb 2015
Ort: Halle/S.
116 Beiträge
 
Delphi 10.2 Tokyo Enterprise
 
#5

AW: Prozedur einer Variablen zuweisen

  Alt 17. Jan 2017, 14:01
Dann nochmal mit Code - Konsolenanwendung, damit man es schnell nachgebastelt kriegt.

DPR:

Delphi-Quellcode:
program Project1;

uses
  System.SysUtils,
  Unit1 in 'Unit1.pas',
  Unit2 in 'Unit2.pas';

var
  X: TFoo;
begin
  Demonstration;
  WriteLn('');
  X := TFoo.Create;
  try
    X.Demonstration;
  finally
    X.Free;
  end;
  ReadLn;
end.
Unit 1 - direkte Funktionsaufrufe

Delphi-Quellcode:
unit Unit1;

interface

type
  TNodeFunction = function(AInput: String): String;

function Func1(AInput: String): String;
function Func2(AInput: String): String;
function Func3(AInput: String): String;
procedure Demonstration;


implementation

uses
  System.SysUtils;

function Func1(AInput: String): String;
begin
   result := AInput;
end;

function Func2(AInput: String): String;
begin
   result := 'Fooled You';
end;

function Func3(AInput: String): String;
begin
   result := UpperCase(AInput);
end;

procedure Demonstration;
var
  SomeFunc, SomeOtherFunc: TNodeFunction;
begin

     SomeOtherFunc := Func3;

     SomeFunc := Func1;
     WriteLn(SomeFunc('Hello')); // returns 'Hello'
     SomeFunc := Func2;
     WriteLn(SomeFunc('Hello')); // returns 'Fooled You'

     WriteLn(SomeOtherFunc('lower case')); // returns 'LOWER CASE'

end;

end.
Unit 2 - Objektmethoden

Delphi-Quellcode:
unit Unit2;

interface

type
  TNodeFunction = function(AInput: String): String of object;

  TFoo = class
    function Func1(AInput: String): String;
    function Func2(AInput: String): String;
    function Func3(AInput: String): String;
    procedure Demonstration;
  end;

implementation

uses
  System.SysUtils;

procedure TFoo.Demonstration;
var
  SomeFunc, SomeOtherFunc: TNodeFunction;
begin
   SomeOtherFunc := Func3;

   SomeFunc := Func1;
   WriteLn(SomeFunc('Hello')); // returns 'Hello'
   SomeFunc := Func2;
   WriteLn(SomeFunc('Hello')); // returns 'Fooled You'

   WriteLn(SomeOtherFunc('lower case'));
end;

function TFoo.Func1(AInput: String): String;
begin
  result := AInput;
end;

function TFoo.Func2(AInput: String): String;
begin
  result := 'Fooled You';
end;

function TFoo.Func3(AInput: String): String;
begin
  result := UpperCase(AInput);
end;

end.
Sebastian
  Mit Zitat antworten Zitat