ERLEDIGT
NEIN
NEIN
ANTWORTEN
2
2
ZUGRIFFE
252
252
EMPFEHLEN
-
09.09.10 13:23 #1
- Registriert seit
- May 2004
- Beiträge
- 684
Hallo,
ich habe hier 2 Enums, wobei das erste die Kategorien für die Werte des zweiten stehen:
- Automarke
- Modell
Es gibt z.b. "Automarke.AUDI", wobei es dann "Modell.A6" gibt. Nun wird ja ganz deutlich, dass das zweite Enum quasi fachlich abhängig vom ersten ist, da das erste ja die Kategorie darstellt.
Gibt es vielleicht eine geschicktere Möglichkeit, z.b. mit einer EnumMap o.Ä., diese beiden Enums tatsächlich zu verschachteln, sodass man quasi Automarke.AUDI.A8 oder so schreiben kann? Wie würdet Ihr diese beiden Enums aufbauen - so wie ich lose gekoppelt nebeneinander her existieren lassen oder vielleicht doch was mit einer EnumMap?
Über Eure Ideen würde ich mich sehr freuen
Vielen Dank!
-
Hallo!
Warum brauchst du denn unbedingt Enums? Enums sind toll, aber hier meiner Meinung nach nicht das Mittel der Wahl...
Bau dir zwei "echte" Klassen "Automarke" und "Modell", wobei jede Automarke eine Liste von Modellen beinhaltet. Das ist meiner Meinung nach sauber und spätestens, wenn du irgendwann dein Datenmodell erweiten musst, wirst du dich über diese saubere Implementierung freuen.
Gruß,
RoCMe
-
09.09.10 17:20 #3
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.886
- Blog-Einträge
- 29
Hallo,
schau mal hier:
Code java: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 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
package de.tutorials.training; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import de.tutorials.training.Brands.Audi.AudiType; import de.tutorials.training.Brands.Mercedes.MercedesType; public interface Brands { Mercedes MERCEDES = new Mercedes(); public static class Mercedes extends AbstractBrand<MercedesType>{ public static class MercedesType extends BaseType{ public MercedesType(String name) { super(name); } } public static final MercedesType A = new MercedesType("A"); public static final MercedesType B = new MercedesType("B"); public static final MercedesType C = new MercedesType("C"); public static final MercedesType D = new MercedesType("D"); public static final MercedesType E = new MercedesType("E"); public static final MercedesType S = new MercedesType("S"); public static final MercedesType M = new MercedesType("M"); { add(A,B,C,D,E,S,M); } } Audi AUDI = new Audi(); public static class Audi extends AbstractBrand<AudiType>{ public static class AudiType extends BaseType{ public AudiType(String name) { super(name); } } public static final AudiType A3 = new AudiType("A3"); public static final AudiType A4 = new AudiType("A4"); public static final AudiType A5 = new AudiType("A5"); public static final AudiType A6 = new AudiType("A6"); public static final AudiType A7 = new AudiType("A7"); public static final AudiType A8 = new AudiType("A8"); { add(A3,A4,A5,A6,A7,A8); } } public static abstract class AbstractBrand<T extends Type> implements Brand<T>{ protected List<T> types = new ArrayList<T>(); private final String name; public AbstractBrand() { this.name = getClass().getSimpleName(); } protected void add(T ... types){ for(T type : types){ this.types.add(type); } } public Iterator<T> iterator() { return types.iterator(); } public String name() { return name; } @Override public String toString() { return this.name; } } public static class BaseType implements Type{ private final String name; public BaseType(String name) { this.name = name; } public String name() { return this.name(); } @Override public String toString() { return name; } } public static interface Brand<TType extends Type> extends Iterable<TType>{} public static interface Type {} }
Code java: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
package de.tutorials.training; import static de.tutorials.training.Brands.AUDI; import static de.tutorials.training.Brands.MERCEDES; import static java.util.Arrays.asList; import de.tutorials.training.Brands.Audi.AudiType; import de.tutorials.training.Brands.Brand; import de.tutorials.training.Brands.Mercedes.MercedesType; import de.tutorials.training.Brands.Type; public class HandMadeEnumExample { public static void main(String[] args) { System.out.println(Brands.Mercedes.A); for(MercedesType type : MERCEDES){ System.out.println(type); } /* Compile Fehler... :) for(MercedesType type : Brands.Audi){ System.out.println(type); } */ for(AudiType type : AUDI){ System.out.println(type); } System.out.println("#######"); for(Brand<? extends Type> brand : asList(MERCEDES, AUDI)){ System.out.println("Brand: " + brand); for(Type type : brand){ System.out.println(type); } } } }
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
Ähnliche Themen
-
Datensatz geschickt kopieren.
Von topf im Forum Relationale DatenbanksystemeAntworten: 3Letzter Beitrag: 12.12.07, 15:57 -
Geschickt IE- Größen-Bug umgehen ?
Von cosmanova im Forum CSSAntworten: 8Letzter Beitrag: 21.09.07, 14:35 -
Geschickt freistellen.
Von Vale-Feil im Forum PhotoshopAntworten: 10Letzter Beitrag: 11.07.05, 21:21 -
Forum: Kategorie und dann Themen dieser Kategorie ausgeben
Von the snake II im Forum PHPAntworten: 2Letzter Beitrag: 20.04.05, 21:12 -
Kategorie ist Unterkategorie von Kategorie, usw.
Von muhkuh im Forum PHPAntworten: 6Letzter Beitrag: 21.05.04, 18:55





Zitieren

Login





