java反射机制

来源:互联网 发布:云计算公司排名 编辑:程序博客网 时间:2024/05/29 19:23

java反射经常应用于搭建框架,深入理解java反射有助于理解各种框架的工作原理。以下是学习java反射时的写的一些简单demo:

package com.abner.study;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.junit.Test;

public class demo6 {
/**
*@author Abner
*@throws Exception
*@desc 反射
*反射主要应用于搭建框架,主要包括两个步骤,1、加载类;2、解剖(反射)构造方法、方法、属性
**/

//加载类,目前java有三种加载类的方法public void loadClass() throws Exception{    //1 一般建议使用这种方式加载类    Class cls1 = Class.forName("com.abner.study.Person");    //2    Class cls2 = new Person().getClass();    //3    Class cls3 = Person.class;}//反射类的无参构造方法 public Person()@Testpublic void test() throws Exception{    //加载类    Class cls1 = Class.forName("com.abner.study.Person");    //反射构造方法    Constructor c = cls1.getConstructor(null);    //创造构造方法实例    c.newInstance(null);}//反射类的字符串参数构造方法 public Person(String name)@Testpublic void test1() throws Exception{    //加载类    Class cls1 = Class.forName("com.abner.study.Person");    //反射构造方法    Constructor c = cls1.getConstructor(String.class);    //创造构造方法实例    c.newInstance("abner");}//反射类的混合参数构造方法public Person(int age,String name)@Testpublic void test2() throws Exception{    //加载类    Class cls1 = Class.forName("com.abner.study.Person");    //反射构造方法    Constructor c = cls1.getConstructor(int.class,String.class);    //创造构造方法实例    c.newInstance(23,"abner");}//反射类的方法public void run(String oprator)@Testpublic void test3() throws Exception{    Person p = new Person();    //加载类    Class cls1 = Class.forName("com.abner.study.Person");    //反射类中的执行操作的一般方法,注意和反射构造方法时的区别,此处使用getMethod,而非getConstructor    Method m = cls1.getMethod("run", String.class);    //调用方法    m.invoke(p, "run");}//反射类私有的方法private void eat(String oprator)@Testpublic void test4() throws Exception{    Person p = new Person();    //加载类    Class cls1 = Class.forName("com.abner.study.Person");    //反射类中私有的方法,注意此时使用getDeclaredMethod方法    Method m = cls1.getDeclaredMethod("eat", String.class);    //私有方法不能被外部类调用,如果需要调用,此时可以暴力破解    m.setAccessible(true);    //调用方法    m.invoke(p, "eat");}//反射类的main方法@Testpublic void test5() throws Exception{    Person p = new Person();    //加载类    Class cls1 = Class.forName("com.abner.study.Person");    //反射类中的main方法    Method m = cls1.getMethod("main", String[].class);    //调用方法,此时需要注意,传递数组时,java会默认将数组拆解,此时会出现参数个数不匹配的现象,所以需要将数组强制转换为对象,阻止java拆解即可    m.invoke(p, (Object)new String[]{"1","2"}); }//反射类的属性public String country="China";@Testpublic void test6() throws Exception{    Person p = new Person();    //加载类    Class cls1 = Class.forName("com.abner.study.Person");    //反射类中的属性    Field f = cls1.getField("country");    //指定获取那个对象的该属性    String country = (String) f.get(p);    System.out.println(country);}//反射类的私有属性private String sex = "man";@Testpublic void test7() throws Exception{    Person p = new Person();    //加载类    Class cls1 = Class.forName("com.abner.study.Person");    //反射类中的私有属性,注意此时使用getDeclaredField方法    Field f = cls1.getDeclaredField("sex");    //暴力破解私有属性    f.setAccessible(true);    //重置该属性值    f.set(p,"famale");    System.out.println(f.get(p));}   

}

0 0
原创粉丝点击