Skip to content

Instantly share code, notes, and snippets.

@Tlaloc-Es
Last active January 4, 2019 11:16
Show Gist options
  • Save Tlaloc-Es/181dd5b1617153215e27b784efe617a9 to your computer and use it in GitHub Desktop.
Save Tlaloc-Es/181dd5b1617153215e27b784efe617a9 to your computer and use it in GitHub Desktop.
public class HierarchyEnum {
public interface Fruit {}
public static enum RedFruit implements Fruit{
RED_APPLE, TOMATOE;
}
public static enum OrangeFruit implements Fruit{
ORANGE, PAPAYA;
}
public static enum GreenVegetable{
LETTUCE;
}
public static enum Fruits {
RED_FRUIT(RedFruit.class),
ORANGE_FRUIT(OrangeFruit.class);
Class<? extends Fruit> classType;
Fruits(Class<? extends Fruit> classType) {
this.classType = classType;
}
public Class<? extends Fruit> getValue() {
return classType;
}
public static Fruit[] getFruitsValues(Fruits fruits) {
return fruits.getValue().getEnumConstants();
}
}
public static void main( String[] args ){
Object[] array = Fruits.getFruitsValues(Fruits.RED_FRUIT);
for(int i = 0; i< array.length; i++) {
System.out.println(array[i]);
}
array = Fruits.getFruitsValues(Fruits.ORANGE_FRUIT);
for(int i = 0; i< array.length; i++) {
System.out.println(array[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment