zl程序教程

您现在的位置是:首页 >  其他

当前栏目

如何通过反射将字符串转换为类

转换反射 如何 字符串 通过
2023-09-14 08:57:41 时间
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();
			}
		
	}

}