Einzelnen Beitrag anzeigen

flosoft

Registriert seit: 23. Apr 2007
15 Beiträge
 
Delphi 2007 Professional
 
#7

AW: Baumstruktur mit ObjectList

  Alt 7. Jun 2012, 23:59
Hallo,

so, habe die Lösung von spaxxn genommen.
Copy&Paste...und funktioniert.

Chapeau!

Habe wieder viel gelernt...

Grüße
flosoft


Hier meine ganz, ganz leicht modifizierte Version von spaxxn:
Delphi-Quellcode:
unit Fuzzy;

interface

uses SysUtils, Contnrs;

type
  TMyObjectList = class;

  TMyObject = class(TObject)
   private
     FName: String;
     FList: TMyObjectList;
     procedure SetName(Value: String);
   public
     constructor Create;
     destructor Destroy; override;
     property Name: String read FName write SetName;
     property Items: TMyObjectList read FList;
   end;

   TMyObjectList = class(TObject)
   private
     FName: String;
     FList: TObjectList;
     procedure SetName(Value: String);
     function GetItem(Index: Integer): TMyObject;
   public
     constructor Create;
     destructor Destroy; override;
     function Add(aItem: TMyObject): Integer;
     procedure Delete(aIndex: Integer);
     function Count: Integer;
     procedure Clear;
     function IndexOf(aItem: TMyObject): Integer;
     property Name: String read FName write SetName;
     property Items[Index: Integer]: TMyObject read GetItem; default;
   end;

implementation


//// TMyObjectList -------------------------------------------------------------
//-------------------------------
constructor TMyObjectList.Create;
//-------------------------------
begin
  inherited Create;
  FList:=TObjectList.Create(false);
end;

//-------------------------------
destructor TMyObjectList.Destroy;
//-------------------------------
begin
   Clear;
   FreeAndNil(FList);
   inherited Destroy;
end;

//----------------------------------------------
procedure TMyObjectList.SetName(Value: String);
//----------------------------------------------
begin
  FName:=Value;
end;

//----------------------------------------------------
function TMyObjectList.Add(aItem: TMyObject): Integer;
//----------------------------------------------------
begin
  Result:=FList.Add(aItem);
end;

//----------------------------------------------
procedure TMyObjectList.Delete(aIndex: Integer);
//----------------------------------------------
var o: TMyObject;
begin
   o:=FList[aIndex] as TMyObject;
   FList.Delete(aIndex);
   o.Free;
end;

//------------------------------------
function TMyObjectList.Count: Integer;
//------------------------------------
begin
  Result:=FList.Count;
end;

//----------------------------
procedure TMyObjectList.Clear;
//----------------------------
begin
  while Count > 0 do Delete(0);
end;

//--------------------------------------------------------
function TMyObjectList.GetItem(Index: Integer): TMyObject;
//--------------------------------------------------------
begin
  Result:=FList[Index] as TMyObject;
end;

//--------------------------------------------------------
function TMyObjectList.IndexOf(aItem: TMyObject): Integer;
//--------------------------------------------------------
begin
  for Result:=0 to Count - 1 do
  begin
    if aItem = Items[Result] then Exit;
  end;
  Result:=-1;
end;


//// TMyObject -----------------------------------------------------------------
//---------------------------
constructor TMyObject.Create;
//---------------------------
begin
   inherited Create;
   FList:=TMyObjectList.Create;
end;

//---------------------------
destructor TMyObject.Destroy;
//---------------------------
begin
  FreeAndNil(FList);
  inherited Destroy;
end;

//------------------------------------------
procedure TMyObject.SetName(Value: String);
//------------------------------------------
begin
  FName:=Value;
end;

end.
... und mein Test (sollte so zu verstehen sein ):
Delphi-Quellcode:
procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
  aObj: TMyObject;
begin
  aObj:=TMyObject.Create;
  aObj.Name:='Heizung';

  aObj.Items.Add(TMyObject.Create);
  aObj.Items[0].Name:='Fuzzy Variable 0(Temperatur)';
  aObj.Items.Add(TMyObject.Create);
  aObj.Items[1].Name:='Fuzzy Variable 1(Druck)';

  Label5.Caption:='Fuzzy Regelung: ' + aObj.Name;
  Label6.Caption:='Anzahl der Fuzzy Variablen: ' + IntToStr(aObj.Items.Count);
  for i:=0 to aObj.Items.Count - 1 do ListBox1.Items.Add(aObj.Items[i].Name);

  aObj.Items[0].Items.Add(TMyObject.Create);
  aObj.Items[0].Items[0].Name:='Fuzzy Term 0(kalt)';
  aObj.Items[0].Items.Add(TMyObject.Create);
  aObj.Items[0].Items[1].Name:='Fuzzy Term 1(lau)';
  aObj.Items[0].Items.Add(TMyObject.Create);
  aObj.Items[0].Items[2].Name:='Fuzzy Term 2(heiss)';

  Label8.Caption:='Anzahl der Fuzzy Terme (hier: Temperatur): ' + IntToStr(aObj.Items[0].Items.Count);
  for i:=0 to aObj.Items[0].Items.Count - 1 do ListBox2.Items.Add(aObj.Items[0].Items[i].Name);

  aObj.Items[0].Items[1].Items.Add(TMyObject.Create);
  aObj.Items[0].Items[1].Items[0].Name:='Eigenschaften/Methoden 0 von Fuzzy Term 1(lau)';
  aObj.Items[0].Items[1].Items.Add(TMyObject.Create);
  aObj.Items[0].Items[1].Items[1].Name:='Eigenschaften/Methoden 1 von Fuzzy Term 1(lau)';
  aObj.Items[0].Items[1].Items.Add(TMyObject.Create);
  aObj.Items[0].Items[1].Items[2].Name:='Eigenschaften/Methoden 2 von Fuzzy Term 1(lau)';
  aObj.Items[0].Items[1].Items.Add(TMyObject.Create);
  aObj.Items[0].Items[1].Items[3].Name:='Eigenschaften/Methoden 3 von Fuzzy Term 1(lau)';

  Label10.Caption:='Anzahl der E/M der Terme (hier: lau): ' + IntToStr(aObj.Items[0].Items[1].Items.Count);
  for i:=0 to aObj.Items[0].Items[1].Items.Count - 1 do ListBox3.Items.Add(aObj.Items[0].Items[1].Items[i].Name);
end;
  Mit Zitat antworten Zitat