提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
通过名称实例化Class
/*来 自 NowJava.com - 时代Java*/ //package com.nowjava; public class Main { public static void main(String[] argv) throws Exception { String clazz = "nowjava.com"; System.out.println(instantiate(clazz)); } public static Object instantiate(String clazz) { try { Class<?> c = Class.forName(clazz); /* 来自 *NowJava.com - 时 代 Java*/ return c.newInstance(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); }