A
AlexL1980
Neues Mitglied
- 0
Hallo! Ich habe folgendes Problem: Ich habe eine Klasse spiel, welche 6x eine Klasse stadt enthält. Wie kann ich diese instanzieren um sie direkt zu benutzen?
Code:
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] [U]spiel[/U] [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]implements[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Serializable {[/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] stadtklasse[] [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]stadt[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] stadtklasse[6]; [/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] spiel() {[/SIZE][/LEFT]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]stadt[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][0].[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]Name[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Hamburg"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; //Hier gibt es den Fehler, weil noch null.[/SIZE][/LEFT]
[LEFT][SIZE=2]}[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
Ich dachte immer mit stadt=new stadtklasse[6] wird der Konstuktor bereits aufgerufen und alle 6 Stadtklassen sind bereits initialsiert... ich könnte mit
stadt[0]=new stadtklasse(); vor stadt[0].Name="Hamburg"; die Klasse initialisieren - aber bei der Vielzahl von späteren Ableitungen sehe ich das als sehr umständlich an... gibt es da keinen Weg dies bereits in der Deklaration der Klasse zu tun?
Die andere Klasse sieht so aus:
Code:
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] stadtklasse {[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]String [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]Name[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]stadtklasse() {[/SIZE][/LEFT]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]Name[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]""[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE][/LEFT]
[SIZE=2]}[/SIZE]
[LEFT][SIZE=2]}[/SIZE][/LEFT]