Last active
January 4, 2019 11:16
-
-
Save Tlaloc-Es/181dd5b1617153215e27b784efe617a9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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