Java 泛型 通用生成器

public class BasicGenerator<T> implements Generator<T>{
	private Class<T> type;
	
	public BasicGenerator(Class<T> type) {
		super();
		this.type = type;
	}

	@Override
	public T next()  {
		try {
			return type.newInstance();
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}
	
	public static <T> Generator<T> create(Class<T> type){
		return new BasicGenerator<T>(type);
	}
}
interface Generator<T> {
	T next();
}

你可能感兴趣的:(java 泛型)