zoukankan      html  css  js  c++  java
  • 反射的使用场景

    1.运行环境

    JDK8+lntellij IDEA 2018.3

    2.Reflection(API)

    在JDK中,主要由以下类来实现Java反射机制,这些类都位于java.lang.reflect包中:

    • Class类:代表一个类。
    • Field 类:代表类的成员变量(成员变量也称为类的属性)。
    • Method类:代表类的方法。
    • Modifier类:代表修饰符。
    • Constructor 类:代表类的构造方法。
    • Array类:提供了动态创建数组,以及访问数组的元素的静态方法

    3.Class中的常用方法(获得类的构造方法)

    • getConstructors():获得类的所有的public类型的构造方法。
    • getDeclaredConstructors():获得类所有的构造方法
    • getConstructor(Class[] parameterTypes):获得类的特定public的构造方法, parameterTypes 参数指定构造方法的参数类型。
    • getDeclaredConstructor(Class[] parameterTypes):获得类指定的所有的构造方法

    4.Class中的常用方法(获得类的属性)

    • getFields():获得类的public类型的属性。
    • getDeclaredFields():获得类本身的所有属性
    • getField(String):获得指定名字的public类型的属性对象
    • getDeclaredField(String)获得类自己的指定名字的属性对象

      Field类:

    • getModifiers():获得属性的修饰符(Modifier.toString(int)显示对应的修饰符类型)
    • getType() : 获得属性的类型 

    5.Class中的常用方法(获得类的方法)

    • getMethods():获得类的public类型的方法。
    • getDeclaredMethods():获得类的所有方法。
    • getMethod(String name, Class[] parameterTypes):获得类的特定public的方法, name参数指定方法的名字, parameterTypes 参数指定方法的参数类getDeclaredMethod(String name, Class[] parameterTypes)

      Method类:

    • getParameterTypes() :获得方法的所有参数类型
    • getReturnType():获得方法的返回值类型

    Ø获取构造函数(方法)

    public class Student {
        private String name;
        private  int age;
    
        public Student() {
            super();
        }
    
        public Student(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 "Student{" +
                    "name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }

    ØJava代码

    public class Demo2 {
        public static void main(String[] args) {
            Student student=new Student();
            Class clastu= student.getClass();
            Field[] fields=clastu.getDeclaredFields();
            System.out.println(Arrays.toString(fields));
    
            try {
                Field field=clastu.getDeclaredField("name");
                System.out.println("field"+field);
                Field[] fields1=clastu.getDeclaredFields();
                System.out.println("fields1"+fields1);
                System.out.println(Arrays.toString(fields1));
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            }
            try {
    
                Method method=clastu.getMethod("test",String.class);
                System.out.println("*****************************");
                System.out.println("method"+method);
                System.out.println("*****************************");
                Method[] methods=clastu.getMethods();
                System.out.println("methods"+methods);
                System.out.println(Arrays.toString(methods));
                System.out.println("**********************************");
                Method[]  methods1=clastu.getMethods();
                System.out.println("methods"+Arrays.toString(methods1));
    
                //4.modifiler
                int num= clastu.getModifiers();
                System.out.println("num:"+num);
    
                //5.Constructor
                Constructor constructor=clastu.getConstructor(String.class);
                System.out.println("constructor:"+constructor);
    
                //6.Constructers
                Constructor[] constructors=clastu.getConstructors();
                System.out.println("constructors:"+constructors);
                System.out.println("***********************************");
                System.out.println(Arrays.toString(constructors));
    
                //7.
                Constructor[] constructors1 =clastu.getDeclaredConstructors();
                System.out.println("****************************");
                System.out.println(Arrays.toString(constructors1));
    
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            }
        }
    }

    总结:

    这些类都是在reflect反射包中提供的,

    这些类有助于我们获取类以及类中的属性,方法,访问修饰符,从而让我们能够得到类相关的具体信息.


    心得:

      在现实生活中,我们常听到的关于时间的话有很多:一寸光阴一寸金,寸金难买寸光阴、时间就是金钱等等。

      其实,时间不仅仅是金钱!时间远比金钱更宝贵,更有价值。

      管理好自己时间的真正意义便是为了避免浪费更多的时间,

      也就是尽量安排得让我们能够在同一时间做更多的事情,提高自己使用时间的效率,提高学习效率~

      你所浪费的今天,是昨天死去的人奢望的明天。你所厌恶的现在,是未来的你回不去的曾经。

  • 相关阅读:
    Java设计模式(十二) 策略模式
    Java设计模式(二) 工厂方法模式
    Java设计模式(一) 简单工厂模式不简单
    Kafka设计解析(四)- Kafka Consumer设计解析
    Kafka设计解析(三)- Kafka High Availability (下)
    Kafka设计解析(二)- Kafka High Availability (上)
    Spark 灰度发布在十万级节点上的成功实践 CI CD
    Spark SQL / Catalyst 内部原理 与 RBO
    Java进阶(七)正确理解Thread Local的原理与适用场景
    Kafka设计解析(八)- Exactly Once语义与事务机制原理
  • 原文地址:https://www.cnblogs.com/dyywht/p/13554922.html
Copyright © 2011-2022 走看看