-
Class与class
假如把class看做是一张图纸的话,那根据图纸加工出来的零件就是一个个的instance(实例)。那你有没有想过,实际上一张具体的图纸也可以看过是某个类型的实例呢?每张图纸是不是都有标题?有作者?有创作日期?是不是存在一个“类”专门来描述它?答案是,有,就是Class!
class是描述对象的,Class是用来描述类的。 -
获取Class
MyObject mObj = new MyObject();
Class clazz = mObj.getClass();
Class clazz2 = Class.forName("com.jeremy.reflection.MyObject ");
- 使用Class
- 实例化对象
Class clazz = Class.forName("com.jeremy.universal_android.reflection.MyObject");
//通过无参构造函数实例化对象
MyObject object = (MyObject) clazz.newInstance();
- 调用其他构造函数
public class MyObject {
final String TAG = "MyObject";
String name;
int index;
public MyObject() {
this.name = "default";
this.index = 0;
}
public MyObject(String str) {
this.name = str;
this.index = 0;
}
public MyObject(String str, int i) {
this.name = str;
this.index = i;
}
public void print() {
LogTool.e(TAG, "name --> " + name + ",index --> " + index);
}
public void printWithParam(String str) {
LogTool.e(TAG, "printWithParam --> " + str);
}
}
MyObject object;
Class clazz = Class.forName("com.jeremy.universal_android.reflection.MyObject");
Constructor<?> constructors[] = clazz.getConstructors();
object = (MyObject) constructors[0].newInstance();
object.print();
object = (MyObject) constructors[1].newInstance("My name is Obj");
object.print();
object = (MyObject) constructors[2].newInstance("My name is Obj", 100);
object.print();
结果为
E/MyObject: name --> default,index --> 0
E/MyObject: name --> My name is Obj,index --> 0
E/MyObject: name --> My name is Obj,index --> 100
- 获取实现的接口
public class MyObject implements Interface1, Interface2 {
}
Class<?> interfaces[] = clazz.getInterfaces();
for (int i = 0; i < interfaces.length; i++) {
LogTool.e(TAG, interfaces[i].getName());
}
E/MyObject: com.jeremy.universal_android.reflection.Interface1
E/MyObject: com.jeremy.universal_android.reflection.Interface2
- 获得父类
Class<?> parents = clazz.getSuperclass();
LogTool.e(TAG, parents.getName());
结果
E/MyObject: java.lang.Object
- 获得完整构造函数的签名
StringBuilder method;
Constructor<?> con;
Constructor<?> cons[] = clazz.getConstructors();
for (int i = 0; i < cons.length; i++) {
method = new StringBuilder();
con = cons[i];
// 获取修饰符
int modifier = con.getModifiers();
method.append(Modifier.toString(modifier));
// 获取构造函数名称
method.append(" ").append(con.getName());
method.append("(");
// 获取构造函数参数类型
Class<?> params[] = con.getParameterTypes();
for (int j = 0; j < params.length; ++j) {
method.append(params[j].getName() + " arg" + i);
if (j < params.length - 1) {
method.append(",");
}
}
method.append("){}");
LogTool.e(TAG, method.toString());
}
结果
E/MyObject: public com.jeremy.universal_android.reflection.MyObject(){}
E/MyObject: public com.jeremy.universal_android.reflection.MyObject(java.lang.String arg1){}
E/MyObject: public com.jeremy.universal_android.reflection.MyObject(java.lang.String arg2,int arg2){}
- 获得本类属性
Field field = null;
StringBuilder fieldDes = null;
//获得本类属性
Field[] fields = clazz.getDeclaredFields();
//获得父类属性
//Field[] fields = clazz.getFields();
for (int i = 0; i < fields.length; i++) {
fieldDes = new StringBuilder();
field = fields[i];
// 获取修饰符
int modifier = field.getModifiers();
fieldDes.append(Modifier.toString(modifier));
Class<?> type = field.getType();
fieldDes.append(" " + type.getName() + " " + field.getName());
LogTool.e(TAG, fieldDes.toString());
}
结果
E/MyObject: public final java.lang.String TAG
E/MyObject: public java.lang.String name
E/MyObject: public int index
- 通过反射调用方法
//通过反射调用方法
Method method1 = clazz.getMethod("print");
method1.invoke(clazz.newInstance());
//调用有参数的方法
method1 = clazz.getMethod("printWithParam", String.class);
method1.invoke(clazz.newInstance(), "hello world");
结果
E/MyObject: name --> default,index --> 0
E/MyObject: printWithParam --> hello world
网友评论