反射

1,一个类有多个组成部分,例如:成员变量,成员方法,构造方法等。反射就是加载类,并解剖出类的各个组成部分。
2,java中有一个Class类用于代表某一个类的字节码。
Class类既然代表某个类的字节码,它当然就要提供加载某个类字节码的方法:forName(). forName方法用于加载某个类的字节码到内存中,并使用class对象进行封装。

得到class对象的方式
类名.class
类名.getClass()

    package cn.huaxin.ref;

public class Demo1 {
    /**
     *  反射:加载类,获得类的字节码
     * @param args
     */
    public static void main(String[] args) {
        try {
        //1
            Class clazz=Class.forName("cn.huaxin.ref.Person");
        //2
            Class clazz1 = new Person().getClass();
        //3
            Class clazz2 = Person.class;

        } catch (ClassNotFoundException e) {

            e.printStackTrace();
        }
    }
}

Class对象提供了如下常用方法:

public Constructor getConstructor(Class<?>...parameterType);
public Method getMethod(Class<?>...parameterType);
public Field getFied(Class<?>...parameterType);

这些方法分别用于从类中解剖出构造函数,方法和成员变量。解剖出的成员分别使用Constructor,Method , Field对象表示

package cn.huaxin.ref;


import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;


//解剖类的构造函数,创建类的对象
public class Demo2 {
    //反射无参构造函数:public Person
    @Test
    public void test1() throws Exception{
        Class clazz = Class.forName("cn.huaxin.ref.Person");
        Constructor c=clazz.getConstructor(null);
        Person p =(Person)c.newInstance(null);
        System.out.println(p.name);


    }

    public void test2() throws Exception{
        Class clazz = Class.forName("cn.huaxin.ref.Person");
        Constructor c =clazz.getConstructor(String.class);
        Person  p =(Person)c.newInstance("xxx");
        System.out.println(p.name);
    }
@Test
public void test3() throws Exception{
    Class clazz = Class.forName("cn.huaxin.ref.Person");

    Constructor c =clazz.getConstructor(String.class,int.class);
    Person  p =(Person)c.newInstance("xxx",12);
    System.out.println(p.name);
}
//私有的
@Test
public void test4() throws Exception{
    Class clazz = Class.forName("cn.huaxin.ref.Person");
    //获取私有的构造方法
    Constructor c =clazz.getDeclaredConstructor(List.class);
    c.setAccessible(true);//暴力反射
    Person  p =(Person)c.newInstance(new ArrayList());
    System.out.println(p.name);
}
//创建对象的另外一种途径:以下代码 等效于test1
@Test
public void test5() throws Exception{
    Class clazz = Class.forName("cn.huaxin.ref.Person");
    Constructor c =clazz.getDeclaredConstructor();

    Person  p =(Person)c.newInstance();
    System.out.println(p.name);
    System.out.println(p);
}
}

利用Mthod执行方法
Method 对象提供了如下方法,用于执行它所代表的方法:
public Object invoke(Object obj,Object…args)

    package cn.huaxin.ref;

import java.io.InputStream;
import java.lang.reflect.Method;

import org.junit.Test;

public class Demo3 {
    //反射类的方法: public void aa1(){}
    @Test
    public void test1() throws Exception{
        Person p =new Person();
        Class clazz = Class.forName("cn.huaxin.ref.Person");
        Method method = clazz.getMethod("aa1", null);
        method.invoke(p, null);
    }
    //反射类的方法:  public void aa1(String name,int password){}
    @Test
    public void test2() throws  Exception{
        Person p = new Person();
        Class clazz = Class.forName("cn.huaxin.ref.Person");
        Method method = clazz.getMethod("aa1", String.class,int.class);
        method.invoke(p, "zxx",38);
        //System.out.println("test2");
    }
    //反射类的方法:public Class[] aa1(String name,int[]password){
    @Test
    public void test3() throws  Exception{
        Person p = new Person();
        Class clazz = Class.forName("cn.huaxin.ref.Person");
        Method method = clazz.getMethod("aa1", String.class,int[].class);
    Class cs[]=(Class[])    method.invoke(p, "22",new int[]{1,23});
        System.out.println(cs[0]);
    }
    //反射类 private void aa1(InputStream in){


public void test4() throws  Exception{
        Person p = new Person();
        Class clazz = Class.forName("cn.huaxin.ref.Person");
        Method method = clazz.getDeclaredMethod("aa1",InputStream.class);
        method.setAccessible(true);
        method.invoke(p,null);
    //  System.out.println(cs[0]);
    }
    //反射类 :public static void aa1(int num){
@Test
    public void test5() throws  Exception{

        Class clazz = Class.forName("cn.huaxin.ref.Person");
        Method method = clazz.getMethod("aa1", int.class);
        method.invoke(null, 23);
        //  System.out.println(cs[0]);
    }
//反射类 方法:public static void main(String[] args) {
@Test
    public void tes6() throws Exception{
        Class clazz = Class.forName("cn.huaxin.ref.Person");
        Method method = clazz.getMethod("main",String[].class);
        method.invoke(null, (Object)new String[]{"aa","bb"});
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值