1. 程式人生 > >java根據字串獲取類名

java根據字串獲取類名

程式碼如下


public class Test {
	
	public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		System.out.println(getclass("com.company.interfacetest.Person").getSimpleName());
	}
	
	public static Class getclass(String className) throws InstantiationException, IllegalAccessException, ClassNotFoundException//className是類名
	{
	   Class obj=Class.forName(className); //以String型別的className例項化類
	   return obj;
	}
}

輸出結果

person