【综合练习1】反射ArrayList类(Class Viewer)
在一个类的内部,一般包括域、构造方法、普通方法和内部类等成员。使用反射机制,可以在无法查看源代码的情况下,查看类的成员。请使用反射机制查看ArrayList类中定义的域、构造方法和普通方法。效果如图所示。
/*【综合练习1】反射ArrayList类(Class Viewer)
* 在一个类的内部,一般包括域、构造方法、普通方法和内部类等成员。
* 使用反射机制,可以在无法查看源代码的情况下,查看类的成员。请
* 使用反射机制查看ArrayList类中定义的域、构造方法和普通方法。
* 效果如图所示。
*
* */
package fsyzj;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class Zhlx1 {
@SuppressWarnings("rawtypes")
public static void main(String[] args) throws ClassNotFoundException{
// TODO 自动生成的方法存根
Class<?> clazz = Class.forName("java.util.ArrayList");
System.out.println("类的标准名称:" + clazz.getCanonicalName());
Constructor[] constructors = clazz.getDeclaredConstructors();
System.out.println("类的构造方法:");
if(constructors.length != 0) {
for(Constructor e :constructors) {
System.out.println("\t" + e);
}
}else {
System.out.println("\t空");
}
Field[] fields = clazz.getDeclaredFields();
System.out.println("类的非继承域变量:");
if(fields.length != 0) {
for(Field f :fields) {
System.out.println("\t" + f);
}
}else {
System.out.println("\t空");
}
Method[] method = clazz.getDeclaredMethods();
System.out.println("类的非继承方法:");
if(method.length != 0) {
for(Method m : method) {
System.out.println("\t" + m);
}
}else {
System.out.println("\t空");
}
}
}