Autor Beitrag
Yusha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 20



BeitragVerfasst: Fr 17.09.04 07:44 
Hallo. Ich entwickle ein Datenmodell für einen Baum (TVirtualStringTree) und hab ne
Frage zur Delphi Systax:

Da ich verschiendenste Knoten in diesem Baum habe, implementiere ich verschiedenste
Typen von Klassen die alle von TAbstractNode abgeleitet sind. Nun kann ich die Methode
GetData(Pointer) nicht überschreiben. Von dieser Methodendefinition will ich die Darstellung des Knotens abhängig machen.
Wenn ich die override Direktive anhänge, kommt der Fehler : Statische Methoden können
nicht überschrieben werden. Woran kann ich sehen , das eine Methode statisch ist ?


ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
type TAbstractNode = class
private
  index:Integer;
public
  Childs  :Array of TAbstractNode;
  Name    :String;
  constructor Create;
  procedure AddChild(NewChild:TAbstractNode);
  function HasMore:Boolean;
  function Next:TAbstractNode;
  function First:TAbstractNode;
  function Equals(node:TAbstractNode):Boolean;
  function IndexOf(node:TAbstractNode):Integer;
end;


type TVisualNode = class (TAbstractNode)
protected
  Data:TNodeData;
public
  constructor Create;overload;
  constructor Create(Name:String);overload;
  constructor Create(Strings:Array of WideString);overload;
  constructor Create(Strings:Array of WideString;Colors:Array of TColor);overload;
  procedure   GetData (ptr:PNodeData);
  procedure   AddString(NewString:String;Color:TColor=clWhite);
end;

type TFileNode = class (TVisualNode)
public
  FileName,
  AbsolutePath,
  Extension:String;
  SearchRec:TSearchRec;
  constructor Create(Path:String;SearchRec:TSearchRec);overload;
  constructor Create(Path:String);overload;
  procedure   GetData(ptr:PNodeData); //   <--------- Wiso statisch ?????
  //  procedure   GetData(ptr:PNodeData);override;  <--- funktioniert nicht 
end;

.
.
.

procedure TVisualNode.GetData(ptr:PNodeData);
begin
  ptr.Strings:=Data.Strings;
  ptr.Colors:=Data.Colors;
end;
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Fr 17.09.04 08:46 
Hallo!

Nun, statisch ist jede Methode, die nicht als virtuell deklariert ist. Statische Methoden werden dann anders gehandhabt, um eine bessere Performance zu erreichen. Einfach "virtual;" hinter die Methode der Vorfahrklasse schreiben, dann kannst du in Nachfahren mit "override" diese Methode übersteuern.

Cu,
Udontknow
Yusha Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 20



BeitragVerfasst: Fr 17.09.04 10:13 
Titel: Next
Danke für die schnelle Antwort :-) .

Nächste Frage:

Gibt es zwischen
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
var
node1:TAbstractNode;
node2:TVisualNode; // TVisualNode extends TAbstractNode

begin
node2:=node1 as TVisualNode
end;

und
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
var
node1:TAbstractNode;
node2:TVisualNode; // TVisualNode extends TAbstractNode

begin
node2:=TVisualNode(node1);
end;


Einen Unterschied? Ich habe ab und zu mal ClassCast - Exceptions bekommen bei der ein oder anderen Methode.
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Fr 17.09.04 10:19 
Der AS-Operator prüft, ob die Instanz von der Klasse abgeleitet ist und wirft u.U. dann eine Exception aus, wenn das nicht der Fall ist, während die zweite Variante keine Prüfung vornimmt.

Ich nehme immer die letztere Variante, sodaß ich mir die Prüfung spare.

Cu,
Udontknow
BenBE
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 8721
Erhaltene Danke: 191

Win95, Win98SE, Win2K, WinXP
D1S, D3S, D4S, D5E, D6E, D7E, D9PE, D10E, D12P, DXEP, L0.9\FPC2.0
BeitragVerfasst: Fr 17.09.04 10:24 
Achso: Einwas habt ihr noch vergessen:
Es gibt auch noch dynamic; Funktioniert genauso wie Virtual, wird aber nicht auf Geschwindigkeit, sondern auf Code-Größe optimiert.

_________________
Anyone who is capable of being elected president should on no account be allowed to do the job.
Ich code EdgeMonkey - In dubio pro Setting.
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Fr 17.09.04 10:44 
Das habe ich nicht vergessen, sondern einfach nur aussen vorgelassen, weil´s sowieso so gut wie nie zum Einsatz kommt. :)