模拟电脑的运行,主板运行,对外提供接口PCI,网卡,声卡等设备实现该接口可以完成指定动作,练习中用到的类Mainboard,NetCard,SoundCard,接口PCI
主板:
package Reflect; public class MainBoard { public void run(){ System.out.println("Mainborad run.."); } public void usePCI(PCI p){ if(p!=null){ p.open(); p.close(); } } }
声卡:
package Reflect; public class SoundCard implements PCI { @Override public void open() { System.out.println("SoundCard Run"); } @Override public void close() { System.out.println("SoundCard Stop"); } }
网卡:
package Reflect; public class NetCard implements PCI{ @Override public void open() { System.out.println("NetCard Run"); } @Override public void close() { System.out.println("NetCard Stop"); } }
PCI接口:
package Reflect; public interface PCI { public void open(); public void close(); }
用到的配置文件:
pci1=Reflect.SoundCard pci2=Reflect.NetCard
主类:
package Reflect; import java.io.FileInputStream; import java.io.IOException; import java.util.Properties; public class ReflectTest { /** * @param args * @throws IOException * @throws ClassNotFoundException * @throws IllegalAccessException * @throws InstantiationException */ public static void main(String[] args) throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException { MainBoard mB = new MainBoard(); mB.run(); FileInputStream fis = new FileInputStream("ConfigFile"); Properties prop = new Properties(); prop.load(fis); for (int x = 0; x < prop.size(); x++) { String classname = prop.getProperty("pci" + (x + 1)); Class clazz = Class.forName(classname); PCI p = (PCI) clazz.newInstance(); mB.usePCI(p); } fis.close(); } }
本文为博主原创文章,转载请注明出处:http://www.cnblogs.com/ysw-go/
1、本博客的原创原创文章,都是本人平时学习所做的笔记,如有错误,欢迎指正。
2、如有侵犯您的知识产权和版权问题,请通知本人,本人会即时做出处理文章。
3、本博客的目的是知识交流所用,转载自其它博客或网站,作为自己的参考资料的,感谢这些文章的原创人员