A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

  1. import java.io.FileReader;
  2. import java.io.IOException;
  3. import java.io.Reader;
  4. import java.lang.reflect.Constructor;
  5. import java.lang.reflect.Method;
  6. import java.util.Properties;
  7. /*
  8. *                 写一个配置(properties)文件按记录该类的包名,以及要运行的方法
  9. * 思路:
  10. *                         首先通过properties文件读取该文件, 1
  11. *                         只要通过keys将得到相应的值即可(className,methdeName)2
  12. *                         获得字节码 3
  13. *                         获得构造器,创建实例 4
  14. *                         获得方法 5
  15. *                         设置语法忽略语法检查6
  16. *                         执行方法 7
  17. *
  18. *
  19. *
  20. * */
  21. @SuppressWarnings("all")
  22. public class Text {
  23.                 public static void main(String[] args)throws IOException,Exception {
  24.                         Properties prop=new Properties();
  25.                         prop.load(new FileReader("text.properties"));//1
  26.                         String className= prop.getProperty("className");//2
  27.                         String methdeName=prop.getProperty("methdeName");
  28.                         Class clazz=Class.forName(className);//3
  29.                         Constructor con=clazz.getConstructor(String.class,int.class,int.class,int.class);//4
  30.                         Object ob=con.newInstance("李四",90,90,100);
  31.                         Method sum=clazz.getDeclaredMethod(methdeName, null);//5
  32.                         sum.setAccessible(true);  //6
  33.                         System.out.println(sum.invoke(ob,null));sum.invoke(ob,null);//7
  34.                 }
  35. }
复制代码


1 个回复

倒序浏览
学习了,反射还是有点晕
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马