Java通過反射機制獲得類方法

public class UserName {
    protected void getMethod(String name) {
        System.out.println(name + "成功獲得method");
    }
}
		String classname1 = "com.meicai.test.prototype.UserName";
        Class c1 = Class.forName(classname1);
        Object object = c1.newInstance();
        Method method = c1.getDeclaredMethod("getMethod", String.class);
        method.setAccessible(true);
        method.invoke(object, "張三");

執行結果

張三成功獲得method
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章