java的反射还是挺高能的,用好了能解决好多问题。所以我对一些常用的反射方法做一些总结或整理,看来的永远是记不牢的,我这专门针对各个方法或用法做一个实例来论证。以后哪怕很长的一段时间不用忘记了,也能在看一遍自己写的日志后能快速回想起来。
getMethods()与getDeclaredMethods()
实例:
public class ReflexTest {
public static void main(String[] args) {
Class<U> uClass = U.class;
for (Method method : uClass.getMethods()) {
System.out.println(method.getName());
}
System.out.println("+++++++++++++++++++");
for (Method method : uClass.getDeclaredMethods()) {
System.out.println(method.getName());
}
}
}
class U{
private void privateMethod(){}
void defaultMethod(){}
protected void protectedMethod(){}
public void publicMethod(){}
}
结果:
publicMethod
wait
wait
wait
equals
toString
hashCode
getClass
notify
notifyAll
+++++++++++++++++++
privateMethod
defaultMethod
protectedMethod
publicMethod
总结:
getMethods()是返回该类(包括继承的)所有的public方法。
getDeclaredMethods()是返回该类所有的(private、default、protected和public)的方法(注意:只是该类不包括继承)。
getFields()与getDeclaredFields()
实例:
public class ReflexTest {
public static void main(String[] args) {
Class<U> uClass = U.class;
for (Field field : uClass.getFields()) {
System.out.println(field.getName());
}
System.out.println("+++++++++++++++++++");
for (Field field : uClass.getDeclaredFields()) {
System.out.println(field.getName());
}
}
}
class U{
private int privateField;
int defaultField;
protected int protectedField;
public int publicField;
}
结果:
publicField
+++++++++++++++++++
privateField
defaultField
protectedField
publicField
看上去与上面的Method的区别很类似,getFields()返回的是一个public成员变量。但是因为Object没有public成员变量。所以继续来一个实验:
增加一个Uc类继承U
class Uc extends U{
public int childField;
}
Class<Uc> ucClass = Uc.class;
for (Field field : ucClass.getFields()) {
System.out.println(field.getName());
}
结果;
childField
publicField
总结:
getFields()是返回该类(包括继承的)所有的public字段。
getDeclaredFields()是返回该类所有的(private、default、protected和public)的字段(注意:只是该类不包括继承)。
getField("xxx")与getDeclaredField("xxx")
getField("xxx")实例:
public class ReflexTest {
public static void main(String[] args) {
Class<Uc> ucClass = Uc.class;
try {
ucClass.getField("privateField");
} catch (NoSuchFieldException e) {
System.err.println("can not find privateField");
}
try {
ucClass.getField("defaultField");
} catch (NoSuchFieldException e) {
System.err.println("can not find defaultField");
}
try {
ucClass.getField("protectedField");
} catch (NoSuchFieldException e) {
System.err.println("can not find protectedField");
}
try {
ucClass.getField("publicField");
} catch (NoSuchFieldException e) {
System.err.println("can not find publicField");
}
try {
ucClass.getField("childprivateField");
} catch (NoSuchFieldException e) {
System.err.println("can not find childprivateField");
}
try {
ucClass.getField("childdefaultField");
} catch (NoSuchFieldException e) {
System.err.println("can not find childdefaultField");
}
try {
ucClass.getField("childprotectedField");
} catch (NoSuchFieldException e) {
System.err.println("can not find childprotectedField");
}
try {
ucClass.getField("childpublicField");
} catch (NoSuchFieldException e) {
System.err.println("can not find childpublicField");
}
}
}
class U{
private int privateField;
int defaultField;
protected int protectedField;
public int publicField;
}
class Uc extends U{
private int childprivateField;
int childdefaultField;
protected int childprotectedField;
public int childpublicField;
}
结果:
can not find privateField
can not find defaultField
can not find protectedField
can not find childprivateField
can not find childdefaultField
can not find childprotectedField
总结:
getField("xxx")可以返回该类(包括继承)的public的对应字段字段。
实例:
public class ReflexTest {
public static void main(String[] args) {
Class<Uc> ucClass = Uc.class;
try {
ucClass.getDeclaredField("privateField");
} catch (NoSuchFieldException e) {
System.err.println("can not find privateField");
}
try {
ucClass.getDeclaredField("defaultField");
} catch (NoSuchFieldException e) {
System.err.println("can not find defaultField");
}
try {
ucClass.getDeclaredField("protectedField");
} catch (NoSuchFieldException e) {
System.err.println("can not find protectedField");
}
try {
ucClass.getDeclaredField("publicField");
} catch (NoSuchFieldException e) {
System.err.println("can not find publicField");
}
try {
ucClass.getDeclaredField("childprivateField");
} catch (NoSuchFieldException e) {
System.err.println("can not find childprivateField");
}
try {
ucClass.getDeclaredField("childdefaultField");
} catch (NoSuchFieldException e) {
System.err.println("can not find childdefaultField");
}
try {
ucClass.getDeclaredField("childprotectedField");
} catch (NoSuchFieldException e) {
System.err.println("can not find childprotectedField");
}
try {
ucClass.getDeclaredField("childpublicField");
} catch (NoSuchFieldException e) {
System.err.println("can not find childpublicField");
}
}
}
class U{
private int privateField;
int defaultField;
protected int protectedField;
public int publicField;
}
class Uc extends U{
private int childprivateField;
int childdefaultField;
protected int childprotectedField;
public int childpublicField;
}
结果:
can not find privateField
can not find defaultField
can not find protectedField
can not find publicField
总结:
getDeclaredField("xxx")是返回该类(private、default、protected和public)的字段,但是继承来的字段是无权访问的。
需要访问,只能通过类对象的getSuperclass()获取父类的类对象