Prev: Free Online Subversion Training - Introduction to Subversionfor Developers
Next: Spam on Usenet groups
From: Lew on 29 Apr 2010 19:27 Lew wrote: >> Consider the following: >> >> enum GolfClub >> { >> DRIVER, >> IRON, >> WEDGE, >> PUTTER; >> } >> >> enum OSComponent >> { >> DRIVER, >> DAEMON, >> USER_PROGRAM; >> } >> >> enum ShippingEmployee >> { >> DRIVER, >> PACKER, >> MANAGER, >> CLERK; >> } www wrote: > My original posting was too simplified. A, B, C, D, M, or P all are > related, unlike your example above. Maybe I should call them CarModelA, > CarModelB, ..., CarModelP etc. That in no wise alters the applicability of my points. -- Lew
From: Christian on 30 Apr 2010 12:44 Am 29.04.2010 19:10, schrieb www: > I have three enums: > > enum MyEnum > { > A, B, C, D > } > > enum HisEnum > { > A, B, C, D, M, N > } > > enum HerEnum > { > A, B, C, D, P > } > > A, B, C, and D shows up three times in three enums. I am wondering if > the code can be improved to avoid such repeated coding. Ideally, I would > think MyEnum can be the "parent class" of HisEnum and HerEnum. But enum > cannot sublclass. > > Thank you very much you can allways give the enums a constructor and provide them there with some property.. i.e. MyEnum(boolean hasHerEnumProperty) though the type system won't help you then anymore for preventing usage of wrong classes... though it allows you at least to simply check with some ssertion... i.e. AssertTrue(enum.isHerEnumProperty()); other possibility is use classes instead of enum .. make the constructor private .. and create public static final fields for some example instances ... downside of this is of course you no longer get to use the enums in a switch statement... 3rd possibility: scrap typesafety and use integer constants... Choose what suits you most. Christian
From: Lew on 30 Apr 2010 13:38 Christian wrote: > other possibility is use classes instead of enum .. make the > constructor private .. and create public static final fields for some > example instances ... > downside of this is of course you no longer get to use the enums in a > switch statement... > OP: When you start seeing the same suggestion from different respondents like this, it is an indicator that the solution is at least popular and quite possibly useful, perhaps even just what you're looking for. Christian: What's with all the ".." punctuation? -- Lew
From: Roedy Green on 30 Apr 2010 14:18 On Thu, 29 Apr 2010 13:10:36 -0400, www <www(a)nospam.com> wrote, quoted or indirectly quoted someone who said : >A, B, C, and D shows up three times in three enums. I am wondering if >the code can be improved to avoid such repeated coding. Ideally, I would >think MyEnum can be the "parent class" of HisEnum and HerEnum. But enum >cannot sublclass. see http://mindprod.com/jgloss/enum.html for background. Unfortunately, enums are incompatible with the notion of inheritance. The best you can do is create an enum with all possible values it in, then create subsets with EnumSet. -- Roedy Green Canadian Mind Products http://mindprod.com It�s amazing how much structure natural languages have when you consider who speaks them and how they evolved.
From: Christian on 1 May 2010 04:34 Am 30.04.2010 19:38, schrieb Lew: > Christian wrote: >> other possibility is use classes instead of enum .. make the >> constructor private .. and create public static final fields for some >> example instances ... >> downside of this is of course you no longer get to use the enums in a >> switch statement... >> > > OP: When you start seeing the same suggestion from different > respondents like this, it is an indicator that the solution is at > least popular and quite possibly useful, perhaps even just what you're > looking for. > > Christian: What's with all the ".." punctuation? > > -- > Lew the .. is shot form for ... where ... implies that a though was started that you could elaborate and expand in your mind ... ;-)
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 Prev: Free Online Subversion Training - Introduction to Subversionfor Developers Next: Spam on Usenet groups |