zoukankan      html  css  js  c++  java
  • java中反射

    Person.java===>>person.class ==>>jvm中的类加载器===》》class对象:代表内存中Person.class
    ==>>通过Clsaa对象中的方法获取内存中的 person.class对象的全部成员及方法构造函数等拿到后也都是对象,其构造函数为Constructor对象,变量为Field对象,属性为Method属性
    获取Class对象的三种方式
        1.类名
        2.通过实例
        3.Class.forName("类的全路径");(推荐使用)
     
    package reflect;
     
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
     
    import org.junit.Test;
     
     
     
    /**
     * 反射类的使用
     * @author sunyb
     *
     */
    public class reflect_Test {
     
         
         @Test
         public void run()
         throws ClassNotFoundException
         {
               
                //三种方式获取
                //第一种通过类名获取
                Class clazz0 =Person.class ;
                //第二种通过实例
                Class clazz1 =new Person(1).getClass();
                //第三种通过 Class.forClass
                Class clazz2 =Class.forName( "reflect.Person");
         }
         /**
          * 获取构造对象
          * throws Exception
          */
         @Test
         public void run1()
         throws Exception
         {
                //获取Person的Class对象
                Class clazz=Class.forName ("reflect.Person" );
                //创建实例
                //Person p=(Person)clazz.newInstance();//等于调用了Person的无参构造函数
                //获取有参数的构造器
                Constructor c=clazz.getConstructor(int.class,String. class);
               Person p=(Person) c.newInstance(1, "孙业宝");
               System. out.println(p.getName());
         }
         
         
         /**
          * 获取属性对象
          */
         @Test
         public void run2()
         throws Exception
         {
               
                          //获取Person的Class对象
                          Class clazz=Class.forName("reflect.Person");
                          //创建实例
                         Person p=(Person)clazz.newInstance(); //等于调用了Person的无参构造函数
                         Field name=clazz.getDeclaredField( "name");
                         name.setAccessible( true);
                         name.set(p, "郭美女");
                         System. out.println(name.get(p));
                    
         }
         
         /**
          * 通过反射获取方法
          */
         @Test
         public void run3()
         throws Exception
         {
                //获取Person的Class对象
                Class clazz=Class.forName("reflect.Person" );
                //创建实例
               Person p=(Person)clazz. newInstance();//等于调用了Person的无参构造函数 
               Method m= clazz.getDeclaredMethod ("setName" , String.class);
               m. setAccessible(true);
               m. invoke(p, "凤凰"); //等于执行了 p.Name="凤凰";
               System. out.println(p.getName());
         }
    }
  • 相关阅读:
    chapter4.6生成器
    chapter4.4、递归
    chapter4.3、函数执行流程
    chapter4.2、函数返回值
    直接插入排序
    打印三角型的练习
    杂记
    linux top命令
    makefile 中的 := , += ,?=
    makefile中的shell语法 || Makefile中的@
  • 原文地址:https://www.cnblogs.com/haofaner/p/5623569.html
Copyright © 2011-2022 走看看