Autor Beitrag
Hexenkönig
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 41

Win XP
Delphi 2005 Personal, Delphi 5 Standart
BeitragVerfasst: Do 07.02.08 15:09 
Hi Leuz!
Ich habe da mal eine Frage zu Konstruktorparametern und Attributen einer Klasse:
Erstmal ein bisschen Code:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
TglComponents=class(TglGUI)
 public
  constructor Create(texPath:string; position:TPoint; size:TPoint);
 protected
  texPath:string;
  position:TPoint;
  size:TPoint;
end;

...

constructor TglComponents.Create(texPath:string; position:TPoint; size:TPoint);
begin
 inherited Create;
 ...
end;


Wie man sieht hat der Konstruktor die gleichen Parameter, wie die Klasse Attribute hat.
Wenn ich jetzt den Konstruktor aufrufe, werden dann die Werte der Parameter automatisch an die Attribute zugewiesen?
Ich kann ja jetzt keine Zuweisung a la texPath:=texPath machen.
Falls es nicht automatisch geht, müsste ich dann logischerweise die Parameter anders nennen, aber mich würde es freuen, wenn es gehen würde, da es sehr komfortabel erscheint.

_________________
Irren ist menschlich, aber wenn man richtigen Mist bauen will, braucht man einen Computer.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10185
Erhaltene Danke: 1261

W11x64
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Do 07.02.08 15:12 
Moin!

Da wird nix automatisch zugewiesen, warum auch. :?

Man könnte es so machen: :idea:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
TglComponents=class(TglGUI)
 public
  constructor Create(AtexPath:string; Aposition:TPoint; Asize:TPoint);
 protected
  texPath:string;
  position:TPoint;
  size:TPoint;
end;

...

constructor TglComponents.Create(AtexPath:string; Aposition:TPoint; Asize:TPoint);
begin
 inherited Create;
 texPath := AtexPath;
 ...
end;

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Hexenkönig Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 41

Win XP
Delphi 2005 Personal, Delphi 5 Standart
BeitragVerfasst: Do 07.02.08 15:17 
Ok, Danke..
Das war nur gerade so eine Idee von mir, weil ich beim Zuweisen gemerkt habe, dass ich die Parameter und die Attribute gleich genannt habe.

_________________
Irren ist menschlich, aber wenn man richtigen Mist bauen will, braucht man einen Computer.
Xong
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 113

WIN 2000
Borland Developer Studio 2006
BeitragVerfasst: Do 07.02.08 15:21 
Zur Ergänzung:
Du kannst auch mit dem Self-Schlüsselwort auf die Attribute der Klasse zugreifen. Es bedeutet aber mehr Schreibaufwand. Über den Programmierstil ließe sich auch streiten. Nur so... =)

Narses´ Vorschlag ist die mMn gebräuchlichste Variante; und mindestens genauso gut zu verstehen.

LG,
Xong
Hexenkönig Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 41

Win XP
Delphi 2005 Personal, Delphi 5 Standart
BeitragVerfasst: Do 07.02.08 15:27 
Ich werde es so machen, wie ich es mir gedacht habe und es Narses vergeschlagen hat.
Die Frage hatte ich nur, weil ich in Delphi im Prinzip noch nichts mit Klassen gemacht habe, sondern bisher nur in C++.
Den Thread habe ich auch als beantwortet markiert. Danke

_________________
Irren ist menschlich, aber wenn man richtigen Mist bauen will, braucht man einen Computer.
Xong
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 113

WIN 2000
Borland Developer Studio 2006
BeitragVerfasst: Do 07.02.08 15:32 
In C++ wird das doch auch nicht anders gehandhabt, oder?!

Achja!
Es ist oft sinnvoll Argumente von Konstruktoren - wenn möglich - mit Werten vorzubelegen. Ich nutze das zumindest sehr häufig.
Kroko
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1284

W98 W2k WXP
Turbo D
BeitragVerfasst: Do 07.02.08 17:08 
laut Style-Guide sollten private Variablen mit einem F beginnen!

_________________
Die F1-Taste steht nicht unter Naturschutz und darf somit regelmäßig und oft benutzt werden! oder Wer lesen kann, ist klar im Vorteil!