33 lines
854 B
Java
33 lines
854 B
Java
// enums/cartoons/EnumImplementation.java
|
|
// ©2015 MindView LLC: see Copyright.txt
|
|
// An enum can implement an interface
|
|
package enums.cartoons;
|
|
import java.util.*;
|
|
import com.mindviewinc.util.*;
|
|
|
|
enum CartoonCharacter
|
|
implements Generator<CartoonCharacter> {
|
|
SLAPPY, SPANKY, PUNCHY, SILLY, BOUNCY, NUTTY, BOB;
|
|
private Random rand = new Random(47);
|
|
@Override
|
|
public CartoonCharacter next() {
|
|
return values()[rand.nextInt(values().length)];
|
|
}
|
|
}
|
|
|
|
public class EnumImplementation {
|
|
public static <T> void printNext(Generator<T> rg) {
|
|
System.out.print(rg.next() + ", ");
|
|
}
|
|
public static void main(String[] args) {
|
|
// Choose any instance:
|
|
CartoonCharacter cc = CartoonCharacter.BOB;
|
|
for(int i = 0; i < 10; i++)
|
|
printNext(cc);
|
|
}
|
|
}
|
|
/* Output:
|
|
BOB, PUNCHY, BOB, SPANKY, NUTTY, PUNCHY, SLAPPY, NUTTY,
|
|
NUTTY, SLAPPY,
|
|
*/
|