如果我们用类字面常量重新实现PetCount,那么改写后的结果在许多方面都会显得更加清晰:
//: typeinfo/pets/LiteralPetCreator.java
// Using class literals.
package typeinfo.pets;
import java.util.*;
public class LiteralPetCreator extends PetCreator {
//No try block needed.
@Suppresswarnings(“unchecked”)
public static final List
//Types for random creation:
private static final List
public List
return types;
}
public static void main(String[] args) {
System.out.println(types);
}
}//:~
该示例的输出与PetCount.java相同。



