zoukankan      html  css  js  c++  java
  • Java的反射机制

    反射的概述

    ClassLoader将.class文件加载到JVM中

    反射常用对像的概述

    反射的API

    1 Class类

     

    实体类

    View Code

    测试类

    推荐第三种方式

     1 package com.imooc.reflect.test;
     2 
     3 import org.junit.Test;
     4 
     5 /**
     6  * 
     7  * @author jt
     8  *
     9  */
    10 public class ClassTest {
    11 
    12     @Test
    13     /**
    14      * 获得Class对象
    15      * * 1.通过类名.class
    16      * * 2.对象.getClass()
    17      * * 3.Class.forName();
    18      */
    19     public void demo1() throws ClassNotFoundException{
    20         // 1.通过类名.class的方式
    21         Class clazz1 = Person.class;
    22         // 2.通过对象.getClass()的方式
    23         Person person = new Person();
    24         Class clazz2 = person.getClass();
    25         
    26         // 3.Class类forName();获得(推荐)
    27         Class clazz3 = Class.forName("com.imooc.reflect.test.Person");
    28     }
    29 }
    View Code

    2 Constructor类

    测试类

     1 package com.imooc.reflect.test;
     2 
     3 import java.lang.reflect.Constructor;
     4 
     5 import org.junit.Test;
     6 
     7 public class ConstructorTest {
     8 
     9     @Test
    10     /**
    11      * 获得无参数的构造方法
    12      */
    13     public void demo1() throws Exception{
    14         // 获得类的字节码文件对应的对象:
    15         Class class1 = Class.forName("com.imooc.reflect.test.Person");
    16         Constructor c = class1.getConstructor();
    17         Person person = (Person) c.newInstance();// 相当于Person person = new Person();
    18         // person.eat();
    19     }
    20     
    21     @Test
    22     /**
    23      * 获得有参数的构造方法
    24      */
    25     public void demo2() throws Exception{
    26         Class class1 = Class.forName("com.imooc.reflect.test.Person");
    27         Constructor c = class1.getConstructor(String.class,String.class);
    28         Person person = (Person) c.newInstance("张三","男");// Person person = new Person("张三","男");
    29         System.out.println(person);
    30     }
    31 }
    View Code

    3 Field 类

     

    使用field,实体类没有get,set方法,也能使用.

     测试类

     1 package com.imooc.reflect.test;
     2 
     3 import java.lang.reflect.Field;
     4 
     5 import org.junit.Test;
     6 
     7 public class FieldTest {
     8 
     9     @Test
    10     // 测试公有的属性
    11     public void demo1() throws Exception{
    12         // 获得Class
    13         Class class1 = Class.forName("com.imooc.reflect.test.Person");
    14         // 获得属性:
    15         Field field = class1.getField("name");
    16         // 操作属性:  p.name = "";
    17         Person p = (Person) class1.newInstance();
    18         field.set(p, "李四");// p.name = "李四";
    19         
    20         Object obj = field.get(p);
    21         System.out.println(obj);
    22         
    23     }
    24     
    25     @Test
    26     // 测试私有的属性
    27     public void demo2() throws Exception{
    28         // 获得Class
    29         Class class1 = Class.forName("com.imooc.reflect.test.Person");
    30         // 获得私有的属性
    31         Field field = class1.getDeclaredField("sex");
    32         // 操作属性:
    33         Person p = (Person) class1.newInstance();
    34         // 私有属性,需要设置一个可访问的权限:
    35         field.setAccessible(true);
    36         field.set(p, "男");
    37         // 获取值:
    38         Object obj = field.get(p);
    39         System.out.println(obj);
    40         System.out.println(p);
    41     }
    42 }
    View Code

    4 Method类

     

    测试类:

     1 package com.imooc.reflect.test;
     2 
     3 import java.lang.reflect.Method;
     4 
     5 import org.junit.Test;
     6 
     7 public class MethodTest {
     8 
     9     @Test
    10     // 测试公有的方法
    11     public void demo1() throws Exception{
    12         Class class1 = Class.forName("com.imooc.reflect.test.Person");
    13         // 实例化:
    14         Person person = (Person) class1.newInstance();
    15         // 获得公有的方法
    16         Method method = class1.getMethod("eat");
    17         // 执行该方法:
    18         method.invoke(person); // person.eat();
    19     }
    20     
    21     @Test
    22     // 测试私有的方法
    23     public void demo2() throws Exception{
    24         Class class1 = Class.forName("com.imooc.reflect.test.Person");
    25         // 实例化:
    26         Person person = (Person) class1.newInstance();
    27         // 获得方法:
    28         Method method = class1.getDeclaredMethod("run");
    29         // 设置私有的属性的访问权限:
    30         method.setAccessible(true);
    31         // 执行该方法:
    32         method.invoke(person, null);
    33     }
    34     
    35     @Test
    36     // 测试私有的方法带参数
    37     public void demo3() throws Exception{
    38         Class class1 = Class.forName("com.imooc.reflect.test.Person");
    39         // 实例化:
    40         Person person = (Person) class1.newInstance();
    41         // 获得该方法:
    42         Method method = class1.getDeclaredMethod("sayHello", String.class);
    43         // 设置访问权限:
    44         method.setAccessible(true);
    45         // 执行:
    46         Object obj = method.invoke(person, "Tom");
    47         System.out.println(obj);
    48     }
    49 }
    View Code
  • 相关阅读:
    Http中的patch
    如何实现腾讯地图的路径规划功能?
    各类数据库分页SQL语法
    ABC222F
    ABC222 G
    LG5308 [COCI2019] Quiz(wqs二分+斜率优化DP)
    [USACO21OPEN] Portals G(Kruskal)
    【做题笔记】SP27379 BLUNIQ
    【做题笔记】CF938C Constructing Tests
    CSP-J/S2021 自闭记
  • 原文地址:https://www.cnblogs.com/superjishere/p/12687533.html
Copyright © 2011-2022 走看看