Problem mit gekapselten Komponenten

Für Fragen zur Programmiersprache auf welcher Lazarus aufbaut
Antworten
u-boot
Beiträge: 308
Registriert: Do 9. Apr 2009, 10:10
OS, Lazarus, FPC: Ubuntu 9.10 (L 0.9.28 FPC 2.2.4)
CPU-Target: 32Bit
Wohnort: 785..

Problem mit gekapselten Komponenten

Beitrag von u-boot »

Code: Alles auswählen

TinParent=class
  end;
 
  TParentClass=class
    FinComponent:TinParent;
    constructor create;
  end;
 
  TinChild=Class(TinParent)
  end;
 
  TChildClass=Class(TParentClass)
  end;
 
implementation
 
constructor TParentClass.create;
begin
  FinComponent:=TinParent.Create;  
// hier sollte bei TChildClass dann FinComponent automatisch vom Typ TInChild sein. Kann man das hier schon  implementieren? Falls ja wie ? 
end;
Ubuntu 9.10 (L 0.9.28 FPC 2.4.x)

MAC
Beiträge: 770
Registriert: Sa 21. Feb 2009, 13:46
OS, Lazarus, FPC: Windows 7 (L 1.3 Built 43666 FPC 2.6.2)
CPU-Target: 32Bit

Re: Problem mit gekapselten Komponenten

Beitrag von MAC »

Klappt folgendes nicht ?

Code: Alles auswählen

constructor TParentClass.create;
begin
if self is TChildClass then FinComponent:=TinChild.Create
  else FinComponent:=TinParent.Create;  
end;

Code: Alles auswählen

Signatur := nil;

u-boot
Beiträge: 308
Registriert: Do 9. Apr 2009, 10:10
OS, Lazarus, FPC: Ubuntu 9.10 (L 0.9.28 FPC 2.2.4)
CPU-Target: 32Bit
Wohnort: 785..

Re: Problem mit gekapselten Komponenten

Beitrag von u-boot »

hmm ok der Punkt geht irgendwie an dich...
Ich hätte gehofft es gibt was schöneres, wo ich die Kindklasse noch nicht an der Stelle kennen muss.
Ubuntu 9.10 (L 0.9.28 FPC 2.4.x)

mse
Beiträge: 2013
Registriert: Do 16. Okt 2008, 10:22
OS, Lazarus, FPC: Linux,Windows,FreeBSD,(MSEide+MSEgui 4.6,git master FPC 3.0.4,fixes_3_0)
CPU-Target: x86,x64,ARM

Re: Problem mit gekapselten Komponenten

Beitrag von mse »

Code: Alles auswählen

type
 
  tinparent = class
  end;
 
  tparentclass = class
   private
    fincomponent: tinparent;
   public
    constructor create; virtual;
  end;
 
  tinchild = class(tinparent)
  end;
 
  tchildclass = class(tparentclass)
   public
    constructor create; override;
  end;
 
{ tparentclass }
 
constructor tparentclass.create;
begin
 if fincomponent = nil then begin
  fincomponent:= tinparent.create;
 end;
end;
 
{ tchildclass }
 
constructor tchildclass.create;
begin
 fincomponent:= tinchild.create;
 inherited;
end;
oder:

Code: Alles auswählen

type
 
  tinparent = class
  end;
  inparentclassty = class of tinparent;
 
  tparentclass = class
   private
    fincomponent: tinparent;
   protected
    class function getincomponentclass: inparentclassty; virtual;
   public
    constructor create;
  end;
 
  tinchild = class(tinparent)
  end;
 
  tchildclass = class(tparentclass)
   protected
    class function getincomponentclass: inparentclassty; override;
  end;
 
{ tparentclass }
 
constructor tparentclass.create;
begin
 fincomponent:= getincomponentclass.create;
end;
 
class function tparentclass.getincomponentclass: inparentclassty;
begin
 result:= tinparent;
end;
 
{ tchildclass }
 
class function tchildclass.getincomponentclass: inparentclassty;
begin
 result:= tinchild;
end;
oder:

Code: Alles auswählen

type
 
  tinparent = class
  end;
  inparentclassty = class of tinparent;
 
  tparentclass = class
   private
    fincomponent: tinparent;
   protected
    function createincomponent: tinparent; virtual;
   public
    constructor create;
  end;
 
  tinchild = class(tinparent)
  end;
 
  tchildclass = class(tparentclass)
   protected
    function createincomponent: tinparent; override;
  end;
 
{ tparentclass }
 
constructor tparentclass.create;
begin
 fincomponent:= createincomponent;
end;
 
function tparentclass.createincomponent: tinparent;
begin
 result:= tinparent.create;
end;
 
{ tchildclass }
 
function tchildclass.createincomponent: tinparent;
begin
 result:= tinchild.create;
end;
oder...

Martin

u-boot
Beiträge: 308
Registriert: Do 9. Apr 2009, 10:10
OS, Lazarus, FPC: Ubuntu 9.10 (L 0.9.28 FPC 2.2.4)
CPU-Target: 32Bit
Wohnort: 785..

Re: Problem mit gekapselten Komponenten

Beitrag von u-boot »

Dankeschön das hilft mir auf jeden Fall weiter.
Ubuntu 9.10 (L 0.9.28 FPC 2.4.x)

Antworten