簡單模仿配置文件的反射機制

//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

相關文章
相關標籤/搜索