作者:じ☆ve宝贝
发布时间:2015-09-11T16:25:25
1.获取指定类对应的Class对象:
getClass()方法、forName()静态方法、类名.class获取对象class对象
2.获取类的全限定名:
getName()例子结果:cn.studyjava.reflect.User,如果为user内的类结果为 cn.studyjava.reflect.User$A
getSimpleName()例子结果:User
getCanonicalName()例子结果:cn.studyjava.reflect.User
3.获取类的包名
getPackage()
4.获取类的修饰符
Modifier.toString(method.getModifiers());//获取修饰符
5.获取类的父类
getSuperclass()
6.获取类实现的接口
getInterfaces()
7.获取类的成员变量
Field[] getFields()//获得成员变量名
Field[] getDeclaredFields()//得到子类和父类中公开的成员变量
Field getField("age")//得到自己的声明的所有成员变量
Field getDeclaredField("age")
8.获取类的构造方法
getConstructors()//返回子类父类所有公共构造方法
getDeclaredConstructors()//返回当前声明类的公公构造方法
getDeclaredConstructor(Class... parameterTypes) //返回对象表示类的构造方法
9.获取类的成员方法
Method[] getMethods()//获得父类和子类中的所有共开方法
Method[] getDeclaredMethods()//获得该类所有的方法
Method getMethod("getAge")//获得指定的方法
Method getDeclaredMethod("getAge")