jeudi 10 mars 2016

Java Using Enums to determine class type

I have 3 classes Foo1, Foo2, and Foo3 and an Object can be any one of those 3 types. A random object is given to me with a field in it stating the type. So like:

{
    type: foo1
}

Now I can read that value in just fine and I have an enum class like so:

public enum ObjectType {
    FOO1,
    FOO2,
    FOO3;
}

I want to be able to determine an object's type from the corresponding enum value. So how do I link the enum values to the corresponding classes so that when I read in the type value I can tell if the object is of type Foo1, Foo2, or Foo3.





Aucun commentaire:

Enregistrer un commentaire