Java 根据字符串类名 实例化
程序员文章站
2022-05-23 17:37:18
...
public class Test {
public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
System.out.println(getclass("com.company.interfacetest.User").getSimpleName());
}
public static Class getclass(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException// className是类名
{
Class obj=Class.forName(className); // 以String类型的className实例化类
return obj;
}
}
输出结果:User
上一篇: Java初始化实例