java反射方法

Class c=Class.forName("className");

注明:className必须为全名,也就是得包含包名,比如,cn.netjava.pojo.UserInfo;

Object obj=c.newInstance();//创建对象的实例

OK,有了对象就什么都好办了,想要什么信息就有什么信息了。  
获得构造函数的方法

Constructor getConstructor(Class[] params)//根据指定参数获得public构造器

Constructor[] getConstructors()//获得public的所有构造器

Constructor getDeclaredConstructor(Class[] params)//根据指定参数获得public和非public的构造器

Constructor[] getDeclaredConstructors()//获得public的所有构造器


获得类方法的方法

Method getMethod(String name, Class[] params),根据方法名,参数类型获得方法

Method[] getMethods()//获得所有的public方法

Method getDeclaredMethod(String name, Class[] params)//根据方法名和参数类型,获得public和非public的方法

Method[] getDeclaredMethods()//获得所以的public和非public方法
posted @ 2019-09-26 09:15  清华大咖  阅读(215)  评论(0编辑  收藏  举报