采用反射的方法获取整个类的信息
//c1 = Class.forName("java.util.ArrayList");
package ght06_Class;
import java.sql.SQLOutput;
import java.util.ArrayList;
//reflection
public class getArrayList {
public static void main(String[] args) {
//get ArrayList
ArrayList a = new ArrayList();
Class c1 = null;
//get ArrayList by reflection
try {
c1 = Class.forName("java.util.ArrayList");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
System.out.println("ArrayList: " + c1);
//使用反射机制查看ArrayList类中定义的成员变量、构造方法和普通方法。
System.out.println("name: " + c1.getName());
System.out.println("getPackageName: " + c1.getPackageName());
System.out.println("Constructor" + c1.getConstructor());
System.out.println("method: "+c1.arrayType());
} catch (Exception e) {
e.printStackTrace();
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)