Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#21

AW: Ich vermisse Konstanten erst im Konstruktor initialisieren zu können

  Alt 9. Apr 2013, 15:50
Diese beiden (Beispiel-) Units sollten veranschaulichen, was geht, und was nicht
Delphi-Quellcode:
unit BaseClass;

interface

type
  TBaseClass0 = class
  strict private
    FStrictPrivate : Integer;
  private
    FPrivate : Integer;
  strict protected
    FStrictProtected : Integer;
  protected
    FProtected : Integer;
  public
    FPublic : Integer;

    procedure DoSomeThing; virtual;
  end;

  TBaseClass1 = class( TBaseClass0 )

  public
    procedure DoSomeThing; override;
  end;

procedure DoSomeThing( ABase : TBaseClass0 );

implementation

{ TBaseClass0 }

procedure TBaseClass0.DoSomeThing;
begin
  FStrictPrivate := 0;
  FPrivate := 0;
  FStrictProtected := 0;
  FProtected := 0;
  FPublic := 0;
end;

{ TBaseClass1 }

procedure TBaseClass1.DoSomeThing;
begin
  inherited;
  // FStrictPrivate := 1; // nicht möglich
  FPrivate := 1;
  FStrictProtected := 1;
  FProtected := 1;
  FPublic := 1;
end;

procedure DoSomeThing( ABase : TBaseClass0 );
begin
  with ABase do
    begin
      // FStrictPrivate := 3; // nicht möglich
      FPrivate := 3;
      // FStrictProtected := 3; // nicht möglich
      FProtected := 3;
      FPublic := 3;
    end;
end;

end.
Delphi-Quellcode:
unit BaseClass2;

interface

uses
  BaseClass;

type
  TBaseClass2 = class( TBaseClass0 )

  public
    procedure DoSomeThing; override;
  end;

procedure DoSomeThing( ABase : TBaseClass0 );

implementation

{ TBaseClass2 }

procedure TBaseClass2.DoSomeThing;
begin
  inherited;
  // FStrictPrivate := 2; // nicht möglich
  // FPrivate := 2; // nicht möglich
  FStrictProtected := 2;
  FProtected := 2;
  FPublic := 2;
end;

procedure DoSomeThing( ABase : TBaseClass0 );
begin
  with ABase do
    begin
      // FStrictPrivate := 4; // nicht möglich
      // FPrivate := 4; // nicht möglich
      // FStrictProtected := 4; // nicht möglich
      // FProtected := 4; // nicht möglich
      FPublic := 4;
    end;
end;

end.
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat