Einzelnen Beitrag anzeigen

TiGü

Registriert seit: 6. Apr 2011
Ort: Berlin
3.070 Beiträge
 
Delphi 10.4 Sydney
 
#6

AW: Automatische Referenzzählug bei Interfaces

  Alt 21. Sep 2021, 12:34
Beispiele (Formatierung teilweise anders):

Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);

{$IFDEF TERM_INTERFACE}
    function createFormula: ITerm;
    var
        op1, op2, power, multOP1: ITerm;
{$ELSE}
    function createFormula: TTerm;
{$ENDIF}
    begin
        op1 := TVariableRTerm.create;
        op2 := TConstantTerm.create(2);
        power := TPowerTerm.create(op1, op2);
        multOP1 := TConstantTerm.create(3);
        result := TMultiplyTerm.create(multOP1, power);
    end;

    function getDerivativeAt(xvalue: double): double;
    var
        term, d: ITerm;
    begin
        term := createFormula;
        d := term.getderivative;
        Result := d.getValue(abs(xvalue));
{$IFDEF TERM_INTERFACE}
        // term:=nil;
        // d:=nil;
{$ELSE}
        term.free;
        d.Free;
{$ENDIF}
    end;
...
Delphi-Quellcode:
{$IFDEF TERM_INTERFACE}
function TMultiplyTerm.getDerivative:ITerm;
var
    newOp1, newOp2, temp: ITerm;
{$ELSE}
function TMultiplyTerm.getDerivative:TTerm;
{$ENDIF}
begin
  newOp1:=TMultiplyTerm.create(fop1.getDerivative, fop2.copy);
  newOp2:=TMultiplyTerm.create(fop1.copy, fop2.getDerivative);
  temp:=TAddTerm.create(newop1.simplify, newop2.simplify);
  result:=temp.simplify;
...
Delphi-Quellcode:
{$IFDEF TERM_INTERFACE}
function TPowerTerm.getDerivative:ITerm;
var
    newop2: ITerm;
    temp: ITerm;
{$ELSE}
function TPowerTerm.getDerivative:TTerm;
{$ENDIF}
begin
  if getSecondOperand.isConstant then
  begin
    newop2:=TPowerTerm.create(getFirstOperand.copy, TConstantTerm.create(getSecondOperand.getValue(0)-1));
    temp:=TMultiplyTerm.create(getSecondOperand.copy, newop2.simplify);
    result:=temp.simplify;
...
  Mit Zitat antworten Zitat