38 lines
1019 B
Java
38 lines
1019 B
Java
// enums/cartoons/EnumImplementation.java
|
|
// (c)2017 MindView LLC: see Copyright.txt
|
|
// We make no guarantees that this code is fit for any purpose.
|
|
// Visit http://OnJava8.com for more book information.
|
|
// An enum can implement an interface
|
|
// {java enums.cartoons.EnumImplementation}
|
|
package enums.cartoons;
|
|
import java.util.*;
|
|
import java.util.function.*;
|
|
|
|
enum CartoonCharacter
|
|
implements Supplier<CartoonCharacter> {
|
|
SLAPPY, SPANKY, PUNCHY,
|
|
SILLY, BOUNCY, NUTTY, BOB;
|
|
private Random rand =
|
|
new Random(47);
|
|
@Override
|
|
public CartoonCharacter get() {
|
|
return values()[rand.nextInt(values().length)];
|
|
}
|
|
}
|
|
|
|
public class EnumImplementation {
|
|
public static <T> void printNext(Supplier<T> rg) {
|
|
System.out.print(rg.get() + ", ");
|
|
}
|
|
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,
|
|
*/
|