public class ReflectProject {
/**
* @param args
* @throws IOException
* @throws Exception
*/
public static void main(String[] args) throws IOException, Exception {
//开启主板
MainBoard mb=new MainBoard();
mb.run();
//创建一个输入流读取配置文件
File config=new File("pci.properties");
FileInputStream fis=new FileInputStream(config);
Properties prop=new Properties();
prop.load(fis);
for(int i=0;i<prop.size();i++){
//通过键获取配置文件中的类名
String PCIname=prop.getProperty("pci"+(i+1));
//获得一个字节码文件对象,并明确是PCI类型
Class<PCI> clazz=(Class<PCI>) Class.forName(PCIname);
//通过该字节码文件对象,获取字节码文件所代表的类的对象
PCI p = clazz.newInstance();
p.open();
p.close();
}
}
}
public class MainBoard {
public void run(){
System.out.println("MainBoard...run");
}
}
public interface PCI {
public void open();
public void close();
}
public class SoundCard implements PCI {
@Override
public void open() {
System.out.println("SoundCard...open");
}
@Override
public void close() {
System.out.println("SoundCard...close");
}
}