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

//Student类python

public class Student {code

public void love() {get

System.out.println("python");

} }io

//Tesy类class

public class Tesy {test

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);

} }static

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

相关文章
相关标签/搜索