简单模仿配置文件的反射机制

原创
2019/01/18 22:12
阅读数 88

//Student类

public class Student {

public void love() {

  System.out.println("python");

} }

//Tesy类

public class Tesy {

public static void main(String[] args) throws Exception {

Properties prop=new Properties();

FileReader f=new FileReader("test.txt");

prop.load(f);

f.close();


String className=prop.getProperty("className");

String methodName=prop.getProperty("methodName");


//反射
Class c=Class.forName(className);

Constructor c1=c.getConstructor();

Object obj=c1.newInstance();

Method m=c.getMethod(methodName);

m.invoke(obj);	

} }

//test.txt className=test.reflection.Student(包名类名) methodName=love

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部