Java反射机制
编程技术  /  houtizong 发布于 3年前   75
package test.reflect;import java.lang.reflect.*;interface Hello{public void say();}class HelloWorld implements Hello{public String msg;public HelloChina helloChina;public void setMsg(String msg) {this.msg=msg;} public void setHelloChina(Hello helloChina) { this.helloChina=(HelloChina) helloChina; } @Override public void say() { System.out.println("Say Hello World!"); }}class HelloChina implements Hello{@Overridepublic void say() {System.out.println("Say Hello China!");}}public class TestReflect{public static void main(String[] args) {Class<?> test= null;try {test = Class.forName("test.reflect.HelloWorld");} catch (Exception e) {e.printStackTrace();}System.out.println("===============本类属性========================");// 取得本类的全部属性Field[] field = test.getDeclaredFields();for (int i = 0; i < field.length; i++) {// 权限修饰符int mo = field[i].getModifiers();String priv = Modifier.toString(mo);// 属性类型Class<?> type = field[i].getType();System.out.println(priv + " " + type.getName() + " " + field[i].getName() + ";");}System.out.println("=========实现的接口或者父类的属性===============");// 取得实现的接口或者父类的属性Field[] filed1 = test.getFields();for (int j = 0; j < filed1.length; j++) {// 权限修饰符int mo = filed1[j].getModifiers();String priv = Modifier.toString(mo);// 属性类型Class<?> type = filed1[j].getType();System.out.println(priv + " " + type.getName() + " " + filed1[j].getName() + ";");} System.out.println("=========实现的方法===============");Method[] methods=test.getMethods();for (int i = 0; i < methods.length; i++) {// 权限修饰符int mo = methods[i].getModifiers();String priv = Modifier.toString(mo);// 属性类型Class<?> type = methods[i].getReturnType();System.out.println(priv + " " + type.getName() + " " + methods[i].getName() + ";");} }}//注:getDeclaredFields() 与 getFields()之间的区别在于前者只列出本类声明属性,后者除了列出本类属性之外还会列出继承属性!
HelloWorld helloWorld=(HelloWorld) test.newInstance(); System.out.println("=========设置之前msg==============="); System.out.println(helloWorld.msg); Method method=test.getMethod("setMsg", String.class); method.invoke(helloWorld, "I am the Msg!! I will Set msg"); System.out.println("=========设置之后msg==============="); System.out.println(helloWorld.msg); HelloChina helloChina=new HelloChina(); Method method1=test.getMethod("setHelloChina", HelloChina.class.getInterfaces()); method1.invoke(helloWorld, helloChina); helloWorld.helloChina.say();//下面就是spring中注入例子:for(Element propertyElement : (List<Element>)element.getChildren("property")) { String name = propertyElement.getAttributeValue("name"); //userDAO String bean = propertyElement.getAttributeValue("bean"); //u Object beanObject = beans.get(bean);//所需要的dao //取得方法名称 String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); System.out.println("method name = " + methodName); //利用反射 Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]); m.invoke(o, beanObject);}
请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!
技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成
网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];
文章归档
文章标签
友情链接