练习11:(2)在typeinfo.pets类库中添加Gerbil,并修改本章中所有示例,让它们适应这个新类。
// Gerbil.java
package typeinfo.pets;
public class Gerbil extends Rodent {
public Gerbil() {}
public Gerbil(String name) {
super(name);
}
}
/*
下面是需要改动的几个类:
// ForNameCreator.java
//: typeinfo/pets/ForNameCreator.java
package typeinfo.pets;
import java.util.*;
public class ForNameCreator extends PetCreator {
private static List<Class<? extends Pet>> types = new ArrayList<Class<? extends Pet>>();
// Types that you want to be randomly created:
private static String[] typeNames = { "typeinfo.pets.Mutt",
"typeinfo.pets.Pug", "typeinfo.pets.EgyptianMau",
"typeinfo.pets.Manx", "typeinfo.pets.Cymric", "typeinfo.pets.Rat",
"typeinfo.pets.Mouse", "typeinfo.pets.Hamster",
"typeinfo.pets.Gerbil" };
@SuppressWarnings("unchecked")
private static void loader() {
try {
for (String name : typeNames)
types.add((Class<? extends Pet>) Class.forName(name));
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
}
}
static {
loader();
}
public List<Class<? extends Pet>> types() {
return types;
}
} // /:~
// LiteralPetCreator.java
//: 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<Class<? extends Pet>> allTypes = Collections
.unmodifiableList(Arrays.asList(Pet.class, Dog.class, Cat.class,
Rodent.class, Mutt.class, Pug.class, EgyptianMau.class,
Manx.class, Cymric.class, Rat.class, Mouse.class,
Hamster.class, Gerbil.class));
// Types for random creation:
private static final List<Class<? extends Pet>> types = allTypes.subList(
allTypes.indexOf(Mutt.class), allTypes.size());
public List<Class<? extends Pet>> types() {
return types;
}
public static void main(String[] args) {
System.out.println(types);
}
} /*
* Output: [class typeinfo.pets.Mutt, class typeinfo.pets.Pug, class
* typeinfo.pets.EgyptianMau, class typeinfo.pets.Manx, class
* typeinfo.pets.Cymric, class typeinfo.pets.Rat, class typeinfo.pets.Mouse,
* class typeinfo.pets.Hamster]
*/// :~
//PetCounter.java
package typeinfo;
import static net.mindview.util.Print.print;
import static net.mindview.util.Print.printnb;
import java.util.HashMap;
import typeinfo.pets.Cat;
import typeinfo.pets.Dog;
import typeinfo.pets.ForNameCreator;
import typeinfo.pets.Gerbil;
import typeinfo.pets.Hamster;
import typeinfo.pets.Manx;
import typeinfo.pets.Mouse;
import typeinfo.pets.Mutt;
import typeinfo.pets.Pet;
import typeinfo.pets.PetCreator;
import typeinfo.pets.Pug;
import typeinfo.pets.Rat;
import typeinfo.pets.Rodent;
public class PetCount {
static class PetCounter extends HashMap<String, Integer> {
private void count(String type) {
Integer quantity = get(type);
if (quantity == null) {
put(type, 1);
} else {
put(type, quantity + 1);
}
}
public static void countPets(PetCreator creator) {
PetCounter counter = new PetCounter();
for (Pet pet : creator.createArray(20)) {
// List each individual pet:
printnb(pet.getClass().getSimpleName() + " ");
if (pet instanceof Pet)
counter.count("Pet");
if (pet instanceof Dog)
counter.count("Dog");
if (pet instanceof Mutt)
counter.count("Mutt");
if (pet instanceof Pug)
counter.count("Pug");
if (pet instanceof Cat)
counter.count("Cat");
if (pet instanceof Manx)
counter.count("EgyptianMau");
if (pet instanceof Manx)
counter.count("Manx");
if (pet instanceof Manx)
counter.count("Cymric");
if (pet instanceof Rodent)
counter.count("Rodent");
if (pet instanceof Rat)
counter.count("Rat");
if (pet instanceof Mouse)
counter.count("Mouse");
if (pet instanceof Hamster)
counter.count("Hamster");
if (pet instanceof Gerbil)
counter.count("Gerbil");
}
// Show the counts:
print();
print(counter);
}
public static void main(String[] args) {
countPets(new ForNameCreator());
}
}
}
你会发现Pet类的结构层次中出现了我们新添加进去的Gerbil类
练习12:(3)将第15章中的CoffeeGenerator.java类用于TypeCounter。
package typeinfo;
import static net.mindview.util.Print.*;
import java.util.Iterator;
import generics.coffee.Coffee;
import generics.coffee.CoffeeGenerator;
import net.mindview.util.TypeCounter;
public class E12_CoffeeCount {
public static void main(String[] args) {
TypeCounter counter = new TypeCounter(Coffee.class);
for (Iterator<Coffee> it = new CoffeeGenerator(20).iterator(); it
.hasNext();) {
Coffee coffee = it.next();
printnb(coffee.getClass().getSimpleName() + " ");
counter.count(coffee);
}
print();
print(counter);
}
}