简谈反射

来源:互联网 发布:中电41所 知乎 编辑:程序博客网 时间:2024/06/06 09:36

所有说明都在注释中

先是测试反射的类

public class simple {public String s="aa";private String s1="cc";public void a1(int i){System.out.println("带int参的方法");}public void a2(String i){System.out.println("带String的方法");}public void simple(){System.out.println("这是构造方法");}public int a3(int i){return i;}private void a4() {System.out.println("私有方法");}public static void main(String[] args){System.out.println("主方法");}}

然后用反射来获取该方法的方法,域,该有构造器

package Main;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.lang.reflect.Modifier;public class Test {//用junit测试该方法,可以运行单个方法@org.junit.Testpublic void test() throws NoSuchMethodException, SecurityException {try { //后面类名要以   包名加类名的方式Class cl=Class.forName("Main.simple");       //用getModifiers方法确定该类的修饰符String modifiers=Modifier.toString(cl.getModifiers());System.out.println(modifiers);//返回1 用该方法以整数形式返回修饰符Integer a=cl.getModifiers();System.out.println(a);//获得类名String name=cl.getName();System.out.println(name);//返回类型名称的字符串String retype=cl.getTypeName();System.out.println(retype);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();}}//反射方法@org.junit.Testpublic void test2() {try {Class cl = Class.forName("Main.simple");Method method=cl.getMethod("a1",int.class);System.out.println(method);/*用该方法调用simple构造器初始化实例Constructor cll=cl.getConstructor(null);simple s=(simple)cll.newInstance(null);System.out.println(s.s);*///实例化对象来调用方法,调用方法要指定实例simple s=new simple();method.invoke(s,1);//获得private的方法//Method mothod2=cl.getDeclaredMethod("simple",null);//getMethod(“方法名”,带的参数类型)//获取该构造方法Method mothod2=cl.getMethod("simple",null);//输出方法名System.out.println(mothod2);//调用该构造方法mothod2.invoke(s,null);//调用有返回型的方法Method method3=cl.getMethod("a3",int.class);System.out.println(method3);method3.invoke(s,10);//private的方法Method method4=cl.getDeclaredMethod("a4",null);method4.setAccessible(true);method4.invoke(s,null);//反射主方法Method method5=cl.getMethod("main",String[].class);System.out.println(method5);//以下两种参数形式都可以//method4.invoke(null,(Object)new String[]{"aa","bb"});method5.invoke(null,new Object[]{new String[]{"aa","bb"}});} catch (ClassNotFoundException e) {e.printStackTrace();} catch (NoSuchMethodException e) {e.printStackTrace();} catch (SecurityException e) {e.printStackTrace();} catch (IllegalArgumentException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}/* catch (InstantiationException e) {// TODO 自动生成的 catch 块e.printStackTrace();}*/}@org.junit.Testpublic void test3(){try {Class cl=Class.forName("Main.simple");//实例化一个对象simple sim=new simple();Field f=cl.getField("s");//获取域的类型Class type=f.getType();System.out.println(type);//获取狱的名字//String name=f.getName();//得到域的值Object value=f.get(sim);     //判断域的类型,因为在此之前不知道该类型if(type.equals(String.class)){String s=(String)value;System.out.println(s);}//获得private的域Field l=cl.getDeclaredField("s1");System.out.println(l.getType());//用该方法获取private的值,这里就不再用条件语句l.setAccessible(true);Object li=l.get(sim);String lii=(String)li;System.out.println(lii);} catch (ClassNotFoundException e) {e.printStackTrace();} catch (NoSuchFieldException e) {// TODO 自动生成的 catch 块e.printStackTrace();} catch (SecurityException e) {// TODO 自动生成的 catch 块e.printStackTrace();} catch (IllegalArgumentException e) {// TODO 自动生成的 catch 块e.printStackTrace();} catch (IllegalAccessException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}


0 0
原创粉丝点击