泛型还可以用于内部类以及匿名内部类
class Customer{
private static long counter=1;
private final long id=counter++;
private Customer(){}
public String toString(){return "Customer"+id;}
public static Generator<Custom> generator(){
return new Generator<Customer>(){
public customer next(){return new Customer();}
};
}
class Teller{
private static long counter=1;
private final long id=counter++;
private Teller(){}
public String toString(){return "Teller"+id;}
public staic Generator<Teller> generator=nw Generator<Teller>{
public Teller next() { return new Teller();}
}
}
本文展示了如何在Java中使用泛型来定义内部类和匿名内部类。通过具体的Customer和Teller类实例,说明了如何创建这些类的生成器,并通过泛型确保类型安全。
501

被折叠的 条评论
为什么被折叠?



