黑马程序员——Java---反射

来源:互联网 发布:qq炫舞淘宝买对戒 编辑:程序博客网 时间:2024/05/03 19:30

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------

package day1;import java.lang.reflect.*;/*反射 * java中可以获得类的字节码文件对象,有三种方法可以获得 *  * *///方法1 类名.class  直接访问程序中的类获得Class文件字节码对象//方法2 实例对象.getClass(); 通过类实例对象的getClass方法获得字节码对象//方法3 Class.forName("java.lang.Enum"); 指定类完整的字符串名称获得其字节码对象/*反射可以获取类当中的public或者非public成员 * * 成员包括   *    成员变量 *    成员方法 *   构造函数 *  * 通过反射可获取当前对象中的 字段(Field) 方法(Method) 构造函数(Constructor) *  * 类中一般有public和非public成员 *  * 但凡是非public获取的都需要配合 setAccessible(true);方法 *  * ********************************************************** * 字段获取 * getField();          //单个字段获取 *  * getDeclaredField(); //单个非public字段获取 *  * getFields();   // 多个字段获取 *  * getDeclaredFields();//非public字段获取 * ********************************************************** * 方法获取 * getMethod();       //单个方法获取 *  * getDelaredMethod();  //单个非public方法获取 *  * getMethods();    //多个方法获取 *  * getDeclaredMethods();//多个非public方法获取 * ******************************************************** * 构造函数获取 * getConstructor();//单个构造函数获取 *  * getDelcaredConstructor();//单个非public构造函数获取 *  * getConstructors();//多个构造函数获取 *  * getDeclaredConstructors();//多个非public构造函数获取 * */class myclass   {private String url;public int age=100;private void show(){  System.out.println("show tool!"); }  public void getInfo(){  System.out.println("getInfo tool!"); } public myclass(String url){this.url=url;System.out.println("Constructor(String url)");}myclass(){System.out.println("Constructor()");}}class test1{public static void main(String[] args)throws Exception{//初始化一个myclass的实例对象myclass mc = new myclass("www.itheima.com");/*********************************************************/     /* 一 (1) 类中被public修饰,字段的(Field)获取*///获取类中public字段ageField f = mc.getClass().getField("age");     //打印该mc对象当中被public修饰的age成员变量值     System.out.println(f.get(mc));/*********************************************************/       /* 一 (2) 类中被非public修饰,字段(Field)的获取*///获取类中private字段urlField fp = mc.getClass().getDeclaredField("url");//设置该字段值的访问权限,true为可访问fp.setAccessible(true);//打印该mc对象当中被private修饰的url成员变量值System.out.println(fp.get(mc));/*********************************************************//*一(3) 类中被public修饰,多个字段(Fields)的获取*///获取类中public多个字段Field[] fs = mc.getClass().getFields();//循环打印该多个字段值for(Field i:fs){System.out.println(i.get(mc));}/*********************************************************//*一(4) 类中被private修饰,多个字段(Fields)的获取*///获取类中public多个字段Field[] fsp = mc.getClass().getDeclaredFields();//循环打印该多个字段值for(Field i:fsp){i.setAccessible(true);System.out.println(i.get(mc));}/*********************************************************/     /* 二 (1) 类中被public修饰,方法的(Method)获取*///获取类中public方法getInfo()Method m = mc.getClass().getMethod("getInfo");//利用mc对象并调用该方法    m.invoke(mc);/*********************************************************/      /* 二 (2) 类中被非public修饰,方法(Method)的获取*///获取类中private方法show()    Method mp = mc.getClass().getDeclaredMethod("show");//设置该方法的访问权限,true为可访问    mp.setAccessible(true);//利用mc对象调用该方法    mp.invoke(mc);/*********************************************************//*二(3) 类中被public修饰,多个方法(Methods)的获取*///获取类中public多个方法Method[] ms = mc.getClass().getMethods();//循环打印该多个方法名称for(Method i:ms){//if(i.getParameterCount()==0)//i.invoke(mc);System.out.println(i.getName());}/*********************************************************//*二(4) 类中被private修饰,多个方法(Methods)的获取*///获取类中private多个方法Method[] msp = mc.getClass().getDeclaredMethods();//利用对象mc循环执行引用方法for(Method i:msp){i.setAccessible(true);i.invoke(mc);}/*********************************************************/    /* 三(1) 类中被public修饰,构造函数的(Constructor)获取*///获取类中public构造函数myclass(String url)Constructor c = mc.getClass().getConstructor(String.class);//利用该字节码对象方法通过mc对象返回一个新的mc对象c.newInstance("public");/*********************************************************/  /* 三 (2) 类中被非public修饰,构造函数的(Constructor)获取*///获取类中private构造函数myclass()Constructor cp = mc.getClass().getDeclaredConstructor();//设置该字段值的访问权限,true为可访问cp.setAccessible(true);//利用该字节码对象方法通过mc对象返回一个新的mc对象cp.newInstance();/*********************************************************//*三(3) 类中被public修饰,多个构造函数的(Constructors)获取*///获取类中public多个构造函数Constructor[] cs = mc.getClass().getConstructors();//利用循环,该字节码对象方法通过mc对象返回多个mc对象for(Constructor i:cs){if(i.getParameterCount()>0)i.newInstance("www.itheima.com");elsei.newInstance();}/*********************************************************//*三(4) 类中被private修饰,多个构造函数的(Constructors)获取*///获取类中private多个构造函数Constructor[] csp = mc.getClass().getDeclaredConstructors();//利用循环,该字节码对象方法通过mc对象返回多个mc对象for(Constructor i:csp){if(i.getParameterCount()>0)i.newInstance("www.itheima.com");elsei.newInstance();}/*********************************************************/}}

0 0
原创粉丝点击