Prev: Free Online Subversion Training - Introduction to Subversionfor Developers
Next: Spam on Usenet groups
From: www on 29 Apr 2010 13:10 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
From: Jeff Higgins on 29 Apr 2010 13:59 On 4/29/2010 1:10 PM, www wrote: > 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 Can you use an EnumSet? You don't say how you intend to use your hierarchy of enums.
From: Lew on 29 Apr 2010 13:59 On Apr 29, 1:10 pm, www <w...(a)nospam.com> wrote: > 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. > Consider the following: enum GolfClub { DRIVER, IRON, WEDGE, PUTTER; } enum OSComponent { DRIVER, DAEMON, USER_PROGRAM; } enum ShippingEmployee { DRIVER, PACKER, MANAGER, CLERK; } All these enums have a constant "DRIVER". In what way would it make sense to have the "DRIVER" constant be common to all three enums? It wouldn't! Just because something has the same name doesn't mean it's the same thing. The whole freaking point of enums is to distinguish same-named things by type so that the names are distinct. Now you want to go and destroy that advantage? Don't forget that enums are classes, and the constants have behaviors. Good luck defining the 'getYardageRange()' of an OSComponent.DRIVER or the 'getTimingClock()' of a GolfClub.DRIVER. Either make an enum that has all the constants it needs, or write your own type-safe enumeration class without using 'enum' so that they can be heritable. -- Lew
From: Lew on 29 Apr 2010 14:05 Lew wrote: > Just because something has the same name doesn't mean it's the same > thing. The whole freaking point of enums is to distinguish same-named > things by type so that the names are distinct. Now you want to go and > destroy that advantage? > See also "Item 34: Emulate extensible enums with interfaces" in Bloch's /Effective Java/, 2nd edition, for a way to simulate what you describe. <http://java.sun.com/docs/books/effective/> -- Lew
From: www on 29 Apr 2010 14:42 Lew wrote: >> > > Consider the following: > > enum GolfClub > { > DRIVER, > IRON, > WEDGE, > PUTTER; > } > > enum OSComponent > { > DRIVER, > DAEMON, > USER_PROGRAM; > } > > enum ShippingEmployee > { > DRIVER, > PACKER, > MANAGER, > CLERK; > } 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.
|
Next
|
Last
Pages: 1 2 3 4 Prev: Free Online Subversion Training - Introduction to Subversionfor Developers Next: Spam on Usenet groups |