![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public class Test06 { public static void main(String[] args) { // TODO Auto-generated method stub String s=instanceT(String::new); Date d=instanceT(Date::new); System.out.println(s); //空字符串 System.out.println(d); //Thu Aug 24 16:06:50 CST 2017 //*************************** String s1=instanceTR(String.class); Date d1=instanceTR(Date.class); System.out.println(s1); //空字符串 System.out.println(d1); //Thu Aug 24 16:08:15 CST 2017 } //利用函数式接口实例化类型变量T,调用者提供一个构造器表达式 public static <T> T instanceT(Supplier<T> constr){ T t=constr.get(); return t; } //利用反射实例化类型变量T,调用者提供一个Class对象 public static <T> T instanceTR(Class<T> c){ T t=null; try { t=c.newInstance(); } catch (Exception e) { t=null; } return t; } }