Re: Question about Typesafe Enums
- From: Hendrik Maryns <hendrik_maryns@xxxxxxxxxxxxx>
- Date: Wed, 14 Dec 2005 17:29:03 +0100
Rhino schreef:
I'm trying to incorporate some of the new features of Java 1.5 into some of my existing classes but I'm somewhat confused about how to do something with specific with typesafe enums.
The main article in the SDK documentation is http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html. Based on that article, I'd like to go a beyond something they show in the Planets example. I need the guidance of those of you who understand this better to tell me if I can do what I want to do. I don't have the terminology straight in my mind yet so forgive me if I don't articulate this correctly.
In the Planet enum that is shown in the article, they have a list of planets and each planet has two specific values, a mass and a radius. The enum provides methods mass() and radius() to get those values when they are needed. Therefore, if I want to get the mass of the Earth, my class can get it via Planet.EARTH.mass(), e.g.
System.out.println("The mass of the Earth is " + Planet.EARTH.mass());
So far so good.
Indeed.
Now, would it be possible to set up other enums to
correspond to the values associated with the planets and use them to reference the values? For example, could I create an enum called Mass that was associated with the mass of each planet, then refer to the mass of a specific planet via the new enum, something like this:
System.out.println("The mass of the Earth is " + Planet.EARTH.Mass);
I'm not wedded to the syntax in that last example; in fact, it looks a bit nasty to me. I'm just trying to indicate that it would use the new Mass enum instead of the mass() method to refer to the first value associated with the given planet.
I don´t think you can do this sort of hard-linking two enums together, but you could define mass() to return something of type Mass, and in the initialisation of Earth tell it to return Mass.EARTH_MASS.
It would be neat to be able to avoid writing methods like mass() and just use additional enums like Mass to refer to the specific facts that I want. Basically, I'd like to be able to use meaningful words like Mass, rather than numbers, as subscripts to the implied array that is in the Planet enum.
Why would you want to have anything to do with those numbers? Although they are used by the JVM internally, there is absolutely no to access them. And how do you see this connected to Mass?
How could I do what I want to do?
What DO you want to do?
And, maybe more importantly, would it be a
good idea or would it cause problems?
Doesn´t seem like a good idea, but you are really unclear.
H.
-- Hendrik Maryns
================== www.lieverleven.be http://aouw.org .
- Follow-Ups:
- Re: Question about Typesafe Enums
- From: Rhino
- Re: Question about Typesafe Enums
- References:
- Question about Typesafe Enums
- From: Rhino
- Question about Typesafe Enums
- Prev by Date: Re: How to use java to finish the following system command?
- Next by Date: Program to list all dirs, subdirs and files older than 1 year
- Previous by thread: Question about Typesafe Enums
- Next by thread: Re: Question about Typesafe Enums
- Index(es):
Relevant Pages
|