R
Rhino
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. 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.
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.
How could I do what I want to do? And, maybe more importantly, would it be a
good idea or would it cause problems?
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. 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.
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.
How could I do what I want to do? And, maybe more importantly, would it be a
good idea or would it cause problems?