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

    Java.lang.reflect库

      Field类:代表类的成员变量(成员变量也称为类的属性)

      Method类:代表类的方法

      Constructor类:代表类的 构造方法

      Array类:提供了动态数组,以及访问数组的元素的静态方法

    通过反射实例化对象

    实例化无参构造函数的对象

      Class.newInstance()

      Class.getConstructor(new Class[]{}).newInstance(new Object[]{})

    实例化带参构造函数的对象

      clazz..getConstructor(Class<?>...parameterTypes).newInstance( Object...initargs)

    通过反射获取并调用方法

       获取当前类以及超类的public Method

      Method[] arrMethods=classType.getMethods();

       获取当前类申明的所有 Method

             Method[] arrMethods=classType.getDeclaredMethods();

       获取当前类以及超类指定的public Method

        Method  methods=classType.getMethods(String name,Class<?>...parameterTypes);

       获取当前类申明的指定的Method

      Method  method=classType.getDeclaredMethods(String name,Class<?>...parameterTypes);

      通过反射动态运行指定的 Method

      Object obj=method.invoke(Object obj,Object...args);

    通过反射获取并调用属性

       获取当前类以及超类的public Field

       Field[] arr Fields=classType.get Fields();

      获取当前类申明的所有 Field

            Field[] arrFields=classType.getDeclaredFields();

      获取当前类以及超类指定的public  Field

        Field   field=classType.getField(String name);

       获取当前类申明的指定的Field

        Field   field=classType.getDeclaredField(String name);

      通过反射动态设定Field值

      field.set(Object obj,Object value);

      通过反射动态获取Field值

      Object obj=field.get(Object obj);

    package com.iotek.classtype;
    
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.Method;
    
    public class ReflectionDemo {
      public static void main(String[] args) throws Exception {
          //获取PerSon类所关联的class对象
          Class<?>class1=Class.forName("com.iotek.classtype.PerSon");
         /* //通过反射机制来构造一个PerSon的实例对象(会默认调用无参数的构造方法)
          PerSon perSon=(PerSon)class1.newInstance();*/
          
          //调用指定的构造方法来构造对象(会调用无参数的构造方法)
          /*Constructor<?> perSon2=class1.getConstructor(new Class[] {});
          PerSon perSon=(PerSon) perSon2.newInstance(new Object[] {});
          System.out.println(perSon);*/
          
          //调用指定的构造方法来构造对象(带参数的构造方法)
          Constructor<?> perSon2=class1.getConstructor(new Class[] {String.class,int.class});
          PerSon perSon=(PerSon) perSon2.newInstance(new Object[] {"张三",20});
          System.out.println(perSon);
          
          //获取class对象所指定的所有方法,包括私有的
          Method[] methods=class1.getDeclaredMethods();
          for (Method method : methods) {
            System.out.println(method.getName());
        }
          
          
          /*//获取class对象所指定的方法,包括私有的(method.setAccessible(true)),破坏面向对象的封装性
          Method method=class1.getDeclaredMethod("toString",new Class[] {});
          System.out.println(method);
          
          //方法的调用
         String string= (String) method.invoke(perSon, new Object[] {});
        System.out.println(string);*/
          
          //获取class对象所指定属性,包括私有的
          Field field=class1.getDeclaredField("name");
          field.setAccessible(true);
          field.set(perSon, "tantanll");
          System.out.println(field.get(perSon));
          
          
    }
    }
    
     class PerSon{
        private String name;
        private int age;
        
        
        public PerSon() {
            
        }
        public PerSon(String name, int age) {
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        @Override
        public String toString() {
            return "PerSon [name=" + name + ", age=" + age + "]";
        }
        
    }
  • 相关阅读:
    洛谷 1339 最短路
    洛谷 1330 封锁阳光大学 图论 二分图染色
    洛谷 1262 间谍网络 Tarjan 图论
    洛谷 1373 dp 小a和uim之大逃离 良心题解
    洛谷 1972 莫队
    洛谷 2158 数论 打表 欧拉函数
    洛谷 1414 数论 分解因数 水题
    蒟蒻的省选复习(不如说是noip普及组复习)————连载中
    关于筛法
    关于整数划分的几类问题
  • 原文地址:https://www.cnblogs.com/tanlei-sxs/p/9998601.html
Copyright © 2011-2022 走看看