一、获取成员变量
引用包路径:java.lang.reflect.filed
获取的方法名:getFileds();获取所有的public的成员变量,并包括父类的成员变量;返回时是个Filed数组
getDeclaredFileds(); 获取所有的public的成员变量,不包括父类的成员变量;返回时是个Filed数组。
语法如下:
Class c =类型.getClass();
Filed[] f = c.getFileds();/c.getDeclaredFileds();
demo如下:
//获取成员变量
Field[] filed = c.getDeclaredFields();
for(Field f : filed)
{
//获取成员变量类型
Class returnType = f.getType();
String strType = returnType.getName();//获取成员类型
String strFiledName =f.getName();//获取成员变量名称
}
二、获取构造函数
引用包路径:java.lang.constructor
获取构造函数:getConstructors();获取所有的public的成员变量,并包括父类的成员变量;返回时是个Constructor数组
getDeclaredConstructors(); 获取所有的public的成员变量,不包括父类的成员变量;返回时是个Constructor数组。
Demo如下:
//获取构造函数
Constructor[] con = c.getDeclaredConstructors();
for(Constructor ctt : con)
{
//获取构函数名称
String strConName =ctt.getName();
//获取构造函数参数类型
Class[] paramType = ctt.getParameterTypes();
for (Class cl : paramType)
{
String paramName= cl.getName();//获取参数类型
}
}