java反射一些方法

来源:互联网 发布:jquery latest.js cdn 编辑:程序博客网 时间:2024/05/20 20:19

1.类字节码文件是在硬盘上存储的,是一个个的.class文件。我们在new一个对象时,JVM会先把字节码文件的信息读出来放到内存中,第二次用时,就不用在加载了,而是直接使用之前缓存的这个字节码信息。

字节码的信息包括:类名、声明的方法、声明的字段等信息。在Java中“万物皆对象”,这些信息当然也需要封装一个对象,这就是Class类、Method类、Field类。

通过Class类、Method类、Field类等等类可以得到这个类型的一些信息,甚至可以不用new关键字就创建一个实例,可以执行一个对象中的方法,设置或获取字段的值,这就是反射技术。

a.在 com.qishui.reflection中新建Test类,测试一些字节码方法。

package com.qishui.reflection;import static java.lang.System.*;//导入类中静态成员import java.lang.reflect.Constructor;import java.lang.reflect.Method;public class Test {    public static void main(String[] args) throws Exception {        // 加载类的字节码        Class<?> cla = Class.forName("com.qishui.reflection.People");        print("第一种方式,推荐 :Class.forName", cla);        print("第二种方式通过对象得到new People().getClass()", new People().getClass());        print("第三种方式通过类获得", People.class);        // 获得类中的一些简单的信息        print("1. getName()类的名称(全名,全限定名)", cla.getName());        print("2 getSimpleName()类的的简单名称(不带包名)", cla.getSimpleName());        print("3. getModifiers(); 类的的修饰符", cla.getModifiers());        // 对于类中的一些方法,和构造函数        For("获得公共的构造函数getConstructors", cla.getConstructors());        For("获得所有的构造函数getDeclaredConstructors", cla.getDeclaredConstructors());        For("获得所有的公共getMethods,包过父类object方法。", cla.getMethods());        For("获得本类所有的方法,不包过父类object方法。", cla.getDeclaredMethods());        // 获取某个参数的构造方法        Constructor constructor = cla.getDeclaredConstructor(String.class);        print("获取一个参数的构造方法", constructor);        // 创建一个对象        People p = (People) constructor.newInstance("jack");        print("对象的hashcode", p.hashCode());        p.eat();        p.sleep(10);        // 无参构造        Constructor constructor2 = cla.getDeclaredConstructor(null);        print("无参构造", constructor2);        // 暴力反射,单例模式失效        constructor2.setAccessible(true);        // 创建一个对象        People p2 = (People) constructor2.newInstance(null);        p2.eat();        print("对象的hashcode", p2.hashCode());        // 获得某一个方法        Method method = cla.getMethod("eat", null);// 方法和参数类型        method.invoke(new People(), null);// 放的是对象那个和参数        print("无参的eat方法", method);        Method m = cla.getMethod("sleep", int.class);        m.invoke(cla.getDeclaredConstructor(String.class).newInstance("tom"), 12);        print("有参sleept方法", m);    }    // 使用泛型简化代码    public static <E> void print(String str, E e) {        out.println(str);        out.println(e);    }    public static <T> void For(String str, T[] t) {        System.out.println();        System.out.println(str);        System.out.println("\n-----遍历开始-----");        // 增强for循坏        for (T ts : t) {            System.out.println(ts);        }        System.out.println("-----遍历结束-----\n");    }}

b.People类的方法

package com.qishui.reflection;public class People {    private String name;    public People(String name) {        this.name = name;    }    public People() {    }    private People(String name,int age) {        System.out.println(this.name+"的年龄为"+age);    }    public String toString() {        return "名字:" + this.name;    }    public void eat() {        System.out.println(this.name + "好会吃....");    }    public void sleep(int hour) {        System.out.println(this.name + "晚上要睡" + hour + "小时 !");    }    private void play(){        System.out.println("paly");    }    public <T> void getLen(T t) {        System.out.println("所传入的参数长度:"+String.valueOf(t).length());    }}

这里写图片描述

c.运行效果

这里写图片描述

这里写图片描述

这里写图片描述

这里写图片描述

0 0
原创粉丝点击