Class类与JAVA反射

来源:互联网 发布:java 微信发送消息 编辑:程序博客网 时间:2024/03/28 23:24

package cn.com.reflect;import java.lang.reflect.Constructor;public class Reflect {private String name;private int age;public Reflect() {super();}public Reflect(String name){this.name = name;}public Reflect(int age){this.age = age;}public static void main(String[] args) throws NoSuchMethodException, SecurityException {Reflect re = new Reflect();//通过Class类的对象clazz访问Reflect对象re的描述信息Class clazz = re.getClass();//获取包路径System.out.println("****************获取包路径********************");System.out.println(clazz.getPackage());//获取类名称System.out.println("****************获取类名称********************");System.out.println(clazz.getName());//获取该类继承的类System.out.println("****************获取该类继承的类********************");System.out.println(clazz.getSuperclass());System.out.println("****************获取所有权限为public的构造方法********************");//获取所有权限为public的构造方法Constructor<Reflect>[] con = clazz.getConstructors();for(int i=0;i<con.length;i++){System.out.println(con[i]);}System.out.println("****************获取权限为public的指定构造方法********************");Constructor<Reflect> con1 = clazz.getConstructor(int.class);System.out.println(con1);}}



1 0
原创粉丝点击