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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 吖玮 中级黑马   /  2016-5-5 20:26  /  555 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

//(1) 写一个Properties格式的配置文件,配置类的完整名称。   
//(2) 写一个程序,读取这个Properties配置文件,获得类的完整名称并加载这个类,用反射 的方式运行run方法。
public class test4 {
        public static void main(String[] args) throws Exception {
                FileReader fr=new FileReader("Properties.txt");
                Properties pt=new Properties();
                pt.load(fr);
                fr.close();
                String className=pt.getProperty("className");
                System.out.println(className);
                Class c=Class.forName(className);
                String run=pt.getProperty("classMethod");
                Constructor con=c.getConstructor();
                Object obj=con.newInstance();
                Method mt=c.getMethod(run);
                mt.invoke(obj);
        }

}
//class DemoClass {
//        public void run()
//    {
//       System.out.println("welcome to heima!");
//    }
//
//}

1 个回复

倒序浏览
楼主加油
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马