package org.entity; /** * 本案例演示如何通过反射将字符串转换为类 * */ public class Test { public static void main(String[] args) { String user = "org.entity.User";//字符串是该类的全限定名 try { Class clzz = Class.forName(user); Object classObj=clzz.newInstance();//将class类转换为对象 System.out.println(classObj);// } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } }