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