一、创建三个类
Person类:
public class Person {
public void eat ( ) {
System. out. println ( "人在吃饭" ) ;
}
}
Student类:
public class Student {
public void study ( ) {
System. out. println ( "学生在学习" ) ;
}
}
Worker类:
public class Worker {
public void job ( ) {
System. out. println ( "上班族在工作" ) ;
}
}
运行主类:
public class Test2 {
public static void main ( String[ ] args) throws Exception {
FileReader fr = new FileReader ( "config2.properties" ) ;
Properties pro = new Properties ( ) ;
pro. load ( fr) ;
fr. close ( ) ;
String className = pro. getProperty ( "className" ) ;
String methodName = pro. getProperty ( "methodName" ) ;
Class c = Class. forName ( className) ;
Object obj = c. newInstance ( ) ;
Method method = c. getMethod ( methodName) ;
method. invoke ( obj) ;
}
}
这里是通过读取配置文件config2.properties文件中的类和方法
config2.properties:
className=cn.itcast.demo1.Student
methodName=study