delphi:language:firsttimecall
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen der Seite angezeigt.
Nächste Überarbeitung | Vorherige Überarbeitung | ||
delphi:language:firsttimecall [2015/03/14 21:31] – angelegt mavarik | delphi:language:firsttimecall [2015/03/14 21:48] (aktuell) – gerdbernau | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
+ | Definition einer Klasse die sich selber kreiert: | ||
+ | |||
+ | <code delphi> | ||
+ | |||
+ | type | ||
+ | TFooClass = class | ||
+ | private | ||
+ | class var _Default: TFooClass; | ||
+ | class destructor Destroy; | ||
+ | class function GetDefault: TFooClass; static; | ||
+ | public | ||
+ | class property Default: TFooClass read GetDefault; | ||
+ | private | ||
+ | Whatever : Integer; | ||
+ | public | ||
+ | constructor Create; | ||
+ | destructor Destroy; override; | ||
+ | |||
+ | procedure DoThis; overload; | ||
+ | end; | ||
+ | </ | ||
+ | |||
+ | Und so wird das ganze definiert! | ||
+ | |||
+ | <code delphi> | ||
+ | |||
+ | { TFooClass } | ||
+ | |||
+ | constructor TFooClass.Create; | ||
+ | begin | ||
+ | inherited Create; | ||
+ | Whatever := 42; // Definieren was man sonst so braucht. | ||
+ | end; | ||
+ | |||
+ | class destructor TFooClass.Destroy; | ||
+ | begin | ||
+ | FreeAndNil( _Default ); | ||
+ | end; | ||
+ | |||
+ | destructor TFooClass.Destroy; | ||
+ | begin | ||
+ | Whatever := -1; | ||
+ | inherited; | ||
+ | end; | ||
+ | |||
+ | procedure TFooClass.DoThis; | ||
+ | begin | ||
+ | Whatever := WhatEver + 1; | ||
+ | end; | ||
+ | |||
+ | class function TFooClass.GetDefault: | ||
+ | begin | ||
+ | if not Assigned( TFooClass._Default ) then | ||
+ | TFooClass._Default := TFooClass.Create; | ||
+ | | ||
+ | Result := TFooClass._Default; | ||
+ | end; | ||
+ | |||
+ | </ | ||
+ | |||
+ | Also kann ich die Classen immer aufrufen mit: | ||
+ | |||
+ | <code delphi> | ||
+ | TFooClass.Default.DoThis; | ||
+ | </ | ||