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

    1.Java反射机制

      反射是Java语言的特征之一。反射机制是 Java 的动态性之一动态语言,在程序运行时,可以改变程序的结构或变量类型。Java 不是动态语言,但具有一定的动态性,可以称为”准动态语言”,具备类似动态语言的特性。Java的动态性让编程变得更加的灵活,功能就更加的强大。

    2.Java反射机制的优点:

      ①允许在运行时动态加载类

      ②可以访问私有属性和私有方法

    class Animal{
        public String name;
        private int age;
        double weight;
        
        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;
        }
    
        public double getWeight() {
            return weight;
        }
    
        public void setWeight(double weight) {
            this.weight = weight;
        }
    
        public Animal() {
            
        }
        
        public Animal(String name) {
            this.name = name;
        }
        
        public Animal(String name,int age) {
            this.name = name;
            this.age = age;
        }
        
        public Animal(String name,int age,double weight) {
            this.name = name;
            this.age = age;
            this.weight = weight;
        }
    }

    3.通过反射获取class对象

      ①使用Object类中的getClass()方法

    //这种创建对象的方式为正射
    Animal a = new Animal();
    Class c3 = a.getClass();

      ②使用Class类的静态方法forName(String className);

    Class c4 = Class.forName("day01.Animal"); 

      ③使用class常量。

    Class c1 = Animal.class;

    4.通过class获取类信息

      ①获取类的包名:getPackage()

    Class c = Class.forName(“day01.Animal”);
    System.out.println("获取包:"+c.getPackage().getName());

      ②获取类的父类:

    System.out.println("父类名:"+c.getSuperclass().getName());

      ③获取类的成员变量:

        Field[] getFields()   //仅公有的

        Field[] getDeclaredFields()  //所有的

    Class c = Class.forName("day01.Animal");
    Object animal = c.newInstance();
    //获取对应属性名的field 对象
    Field field = c.getDeclaredField("name");
    //o代表Animal对象,设置Animal类的属性name的值
    field.set(animal, "小狗");
    System.out.println(field.get(animal));

      ④获取类的构造方法

        Constructor<?>[] getConstructors()

        Constructor<?>[] getDeclaredConstructors()

        Constructor<T> getConstructor(Class<?>... parameterTypes)

        Constructor<T> getDeclaredConstructor(Class<?>... parameterTypes)

    Class c = Class.forName("day01.Animal");
    Object o = c.newInstance();
    Field f = c.getDeclaredField("age");
    //设置是否允许访问私有属性或方法,默认为false,不可访问
    f.setAccessible(true);
    f.set(o, 10);
    System.out.println(f.get(o));
            
    Constructor c1 = c.getConstructor();
    System.out.println(c1);
    Constructor c2 = c.getConstructor(String.class,int.class);
    System.out.println(c2);

      ⑤获取类的成员方法

        Method[] getMethods()

        Method[] getDeclaredMethods()

        Method getMethod(String name, Class<?>... parameterTypes)

        Method getDeclaredMethod(String name, Class<?>... parameterTypes)

    Method[] m = c.getMethods();
    for (Method method : m) {
        System.out.println(method);
    }
    Method m1 = c.getMethod("getAge", null);
    System.out.println(m1);
  • 相关阅读:
    阿里云(一)云存储OSS的命令行osscmd的安装和使用
    Zephir入门教程一
    【转载】视频CDN技术原理与流程说明
    博客园页面css样式
    Linux使用imagemagick的convert命令压缩图片、节省服务器空间
    WebSockets Tutorial(教程一)WebSockets简介
    ngx_lua_API 指令详解(四)ngx.exec指令
    Git与GitHub学习笔记(二)提交的一些笔记
    这些万能的文献检索工具,你用了几个?
    干货||科研收藏夹必备35个学术网址
  • 原文地址:https://www.cnblogs.com/qilin20/p/12377160.html
Copyright © 2011-2022 走看看