java反射技术

一、概述

一个类有多个组成部分,例如:成员变量、成员方法、构造方法等。
反射就是加载类,并解剖出类的各个组成部分。

二、加载类

java中有一个Class类代表某一个类的字节码

方式一
Class clazz = Class.forName("cn.wzk.reflect.Person");

方式二
Class clazz = Person.Class;

方式三
Class clazz = new Person().getClass();

三、解剖类

1.反射类的字段
class Person{
    public String name;
    private int age;

    public void show(){
        System.out.println(this.name);
        System.out.println(this.age);
    }

    //构造方法

    public Person(){
        System.out.println("无参构造方法。。。");
    }

    public Person(String name){
        System.out.println("name :一个参数的构造函数。。。");
    }

    public Person(String name,int age){
        System.out.println("name,age : 两个参数的构造函数。。。。");
    }

    private Person(List list){
        System.out.println("List : 私有构造函数。。。。。");
    }

    //成员方法
    public void method1(){
        System.out.println("method1........");
    }

    public void method1(String name,int age){
        System.out.println(name+"------"+age);
    }

    public String method1(String name,int[] age){
        return name;
    }

    public static void method1(int num){
        System.out.println(num);

    }

    private void method1(InputStream is){
        System.out.println(is);
    }

}
Person p = new Person();
Class clazz= Class.forName("cn.wzk.reflect.Person");
Field name= clazz.getField("name");
name.set(p, "哈哈");

Field age = clazz.getDeclaredField("age");
age.setAccessible(true);
age.set(p,20);
p.show();
2.反射类的构造方法
/**
     * 反射构造函数:public Person()
     */
    @Test
    public void test2() throws Exception{
        Class clazz = Class.forName("cn.wzk.reflect.Person");
        Constructor c = clazz.getConstructor();
        Person p = (Person)c.newInstance();
        System.out.println(p.name);
    }
    /**
     * 反射构造函数:public Person(String name)
     * @throws Exception
     */
    @Test
    public void test3() throws Exception{
        Class clazz = Class.forName("cn.wzk.reflect.Person");
        Constructor c = clazz.getConstructor(String.class);
        Person p = (Person)c.newInstance("小红");

    }
    /**
     * 反射构造函数:public Person(String name,int age)
     * @throws Exception
     */
    @Test
    public void test4() throws Exception{
        Class clazz = Class.forName("cn.wzk.reflect.Person");
        Constructor c = clazz.getConstructor(String.class,int.class);
        Person p =(Person)c.newInstance("Mike",20);
    }

    /**
     * 反射构造函数:private Person(List list)
     * @throws Exception
     */
    @Test
    public void test5() throws Exception{
        Class clazz = Class.forName("cn.wzk.reflect.Person");
        Constructor c = clazz.getDeclaredConstructor(List.class);
        c.setAccessible(true);
        Person p = (Person)c.newInstance(new ArrayList());
    }
3.反射类的成员方法
    /**
     * 反射成员方法:public void method1()
     * @throws Exception
     */
    @Test
    public void test6() throws Exception{
        Class clazz = Class.forName("cn.wzk.reflect.Person");
        Person p = (Person)clazz.newInstance();

        Method method = clazz.getMethod("method1");
        method.invoke(p);
    }

    /**
     * 反射成员方法:public void method1(String name,int age)
     * @throws Exception
     */
    @Test
    public void test7() throws Exception{
        Class clazz = Class.forName("cn.wzk.reflect.Person");
        Person p = (Person)clazz.newInstance();

        Method method = clazz.getMethod("method1",String.class,int.class);
        method.invoke(p,"mike",20);
    }

    /**
     * 反射成员方法:public String method1(String name,int[] age)
     * @throws Exception
     */
    @Test
    public void test8() throws Exception{
        Class clazz = Class.forName("cn.wzk.reflect.Person");
        Person p = (Person)clazz.newInstance();

        Method method = clazz.getMethod("method1",String.class,int[].class);
        Object name =method.invoke(p,"jack",new int[]{1,2,3});
        System.out.println(name);
    }

    /**
     * 反射成员方法:public static void method1(int num)
     * @throws Exception
     */
    @Test
    public void test9() throws Exception{
        Class clazz = Class.forName("cn.wzk.reflect.Person");
        Person p = (Person)clazz.newInstance();

        Method method = clazz.getMethod("method1",int.class);
        method.invoke(p,5);
    }

    /**
     * 反射成员方法:private void method1(InputStream is)
     * @throws Exception
     */
    @Test
    public void test10() throws Exception{
        Class clazz = Class.forName("cn.wzk.reflect.Person");
        Person p = (Person)clazz.newInstance();

        Method method = clazz.getDeclaredMethod("method1",InputStream.class);
        method.setAccessible(true);
        method.invoke(p,new FileOutputStream("a.txt"));
    }
4.反射类的main方法
public static void main(String[] args) {
    System.out.println("mian方法。。。。。");
}
/**
     * 反射类的main方法
     */

    @Test
    public void test11() throws Exception{
        Class clazz = Class.forName("cn.wzk.reflect.Person");
        Method method = clazz.getDeclaredMethod("main",String[].class);
        method.invoke(null, (Object)new String[]{"aa","bb"});
    }

注意
对于反射的参数是数组的情况下,会将数组进行拆开(其实是为了兼容jdk1.4)jdk1.4没有可变数组。

解决
方式一:
method.invoke(null, (Object)new String[]{"aa","bb"});
方式二:
method.invoke(null,new Object[]{new String[]{"aa","bb"}});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值