zoukankan      html  css  js  c++  java
  • java反射详解及说明

    首先写一个Person类:

     1 package lltse.base.reflectdemo;
     2 
     3 public class Person 
     4 {
     5     
     6     private String name ="张三";
     7     
     8     private int sex = 0;
     9     
    10     private int age=10;
    11     
    12     public String id ="中山大道";
    13     
    14     protected int num =123213213;
    15     
    16     public String getName() {
    17         return name;
    18     }
    19 
    20 
    21 
    22     public void setName(String name) {
    23         this.name = name;
    24     }
    25 
    26 
    27 
    28     public int getSex() {
    29         return sex;
    30     }
    31 
    32 
    33 
    34     public void setSex(int sex) {
    35         this.sex = sex;
    36     }
    37 
    38 
    39 
    40     public int getAge() {
    41         return age;
    42     }
    43 
    44 
    45 
    46     public void setAge(int age) {
    47         this.age = age;
    48     }
    49 
    50 
    51 
    52     
    53     
    54     public Person(String name,int sex,int age)
    55     {
    56         this.name = name;
    57         this.sex = sex;
    58         this.age = age;
    59     }
    60     
    61     public Person()
    62     {
    63     }
    64 
    65     /**
    66      * @param args
    67      */
    68     public static void main(String[] args) {
    69         // TODO Auto-generated method stub
    70 
    71     }
    72     
    73     
    74     public int show(String name,int age)
    75     {
    76         System.out.println("showInfo:>>>:name"+name+"age:>>>"+age);
    77         return age;
    78     }
    79 
    80     public static String getName(String name)
    81     {
    82         return name;
    83     }
    84 }

    其次是测试反射的相关方法

    
    
      1 package lltse.base.reflectdemo;
      2 
      3 import java.lang.reflect.Constructor;
      4 import java.lang.reflect.Field;
      5 import java.lang.reflect.InvocationTargetException;
      6 import java.lang.reflect.Method;
      7 
      8 public class ReflectTest {
      9     /**
     10      * @param args
     11      * @throws ClassNotFoundException
     12      * @throws SecurityException
     13      * @throws NoSuchMethodException
     14      * @throws InvocationTargetException
     15      * @throws IllegalArgumentException
     16      * @throws IllegalAccessException
     17      * @throws InstantiationException
     18      */
     19     public static void main(String[] args) throws ClassNotFoundException,
     20             NoSuchMethodException, SecurityException, IllegalAccessException,
     21             IllegalArgumentException, InvocationTargetException,
     22             InstantiationException {
     23         // 获取Class方法一
     24         // Person person = new Person();
     25         // Class personClazz1 = person.getClass();
     26 
     27         // 获取Class方法二
     28         Class personClazz2 = Person.class;
     29 
     30         // 获取Class方法三
     31         Class personClazz3 = Class.forName("lltse.base.reflectdemo.Person");
     32 
     33         // 活动完整的包名和类名
     34         System.out.println("personClazz3.getName():>>>"
     35                 + personClazz3.getName());
     36 
     37         /*
     38          * 返回一个包含某些 Method 对象的数组,这些对象反映此 Class
     39          * 对象所表示的类或接口(包括那些由该类或接口声明的以及从超类和超接口继承的那些的类或接口)的公共 member 方法。
     40          */
     41         Method[] methods = personClazz3.getMethods();
     42 
     43         /*
     44          * 返回 Method 对象的一个数组,这些对象反映此 Class 对象表示的类或接口声明的所有方法,
     45          * 包括公共、保护、默认(包)访问和私有方法,但不包括继承的方法。
     46          */
     47         Method[] declaredMethods = personClazz3.getDeclaredMethods();
     48 
     49         for (int i = 0; i < methods.length; i++) {
     50             System.out.println(methods[i].getName());
     51         }
     52 
     53         // 返回一个包含某些 Constructor 对象的数组,这些对象反映此 Class 对象所表示的类的所有公共构造方法。
     54         Constructor[] constructors = personClazz3.getConstructors();
     55 
     56         for (int t = 0; t < constructors.length; t++) {
     57             System.out
     58                     .println("constructors[" + t + "]:>>>>" + constructors[t]);
     59         }
     60 
     61         // 实例化类对象
     62         Person p = (Person) personClazz3.newInstance();
     63         // 返回一个 Method 对象,它反映此 Class 对象所表示的类或接口的指定公共成员方法。
     64         Method method = personClazz3.getMethod("show", new Class[] {
     65                 String.class, int.class });
     66 
     67         // 对带有指定参数的指定对象调用由此 Method 对象表示的底层方法。p代表被实例化的类对象
     68         int returnValue = (int) method.invoke(p, "zhangsan", 30);
     69         System.out.println("returnValue:>>>" + returnValue);
     70 
     71         /*
     72          * 如果底层方法是静态的,那么可以忽略指定的 obj 参数。该参数可以为 null。 如果底层方法所需的形参数为 0,则所提供的 args
     73          * 数组长度可以为 0 或 null。
     74          */
     75 
     76         Method method2 = personClazz3.getMethod("getName",
     77                 new Class[] { String.class });
     78         String name = (String) method2.invoke(null, "test");
     79         System.out.println("name:>>>" + name);
     80 
     81         
     82         /*获取类中自定义的私有属性*/
     83         /*
     84          * 以上代码中,Field.setAccessible(fields,   true); 是最为关键的一点。
     85             在使用java反射机制获取 JavaBean 的属性值时,如果该属性被声明为private 的,
     86             需要将setAccessible设置为true. 默认的值为false.
     87             如果不设置Accessible为true则无法获取私有属性的值。
     88         */
     89         Field[] fields = personClazz3.getDeclaredFields();
     90         Field.setAccessible(fields, true);// 修改访问控制权限  
     91         
     92         for (int i = 0; i < fields.length; i++) 
     93         {
     94             System.out.println("变量声明类型:"+ fields[i]);
     95             System.out.println("field name:>>>"+fields[i].getName() + "-> ");
     96             System.out.println("field value:>>> "+fields[i].get(new Person()));
     97             
     98         }
     99     }
    100 }
    
    
    
    
    
  • 相关阅读:
    ntp时钟同步
    Office2013中文激活版
    Windows2008|2003超出最大连接数
    Vivaldi浏览器媲美Chrome
    Win10激活KMS2.0
    FTP下载工具
    UltraEdit编辑器|UE
    社工-入侵
    实时系统跟分时系统
    ThreadPoolExecutor线程池解析与BlockingQueue的三种实现
  • 原文地址:https://www.cnblogs.com/lltse/p/5460280.html
Copyright © 2011-2022 走看看