1、通過反射獲取屬性:
class類:
public class Student {
public Student( ){
System.out.println("public Student()");
}
private void haha(){
System.out.println("private void haha()");
}
}
配置文件
class.properties
className=Day01.Student
methodName=haha
獲取方法
//加載屬性文件,取得類名的方法名
Properties props = new Properties();
InputStream is = new FileInputStream("src/Day01/class.properties");
props.load(is);
String className = props.getProperty("className").trim();
String methodName = props.getProperty("methodName").trim();
//通過反射,執行該類的方法
Class c = Class.forName(className);
Constructor con = c.getConstructor(null);
Method m = c.getDeclaredMethod(methodName,null);
m.setAccessible(true); //方式屬性是私有 ,如果是私有必須加上
m.invoke(con.newInstance(null),null);
結果:
public Student()
private void haha()