Notebook class:
Package Cn.fanse;public class Notebook {public void run () {System.out.println (' book Run ');} public void Useusb (USB usb) {Usb.open (); Usb.close ();} }
USB Interface:
Package Cn.fanse;public interface USB {public void open (); public void close ();
USB Interface for Mouse:
Package Cn.fanse;public class Mousebyusb implements usb{@Overridepublic Void Open () {System.out.println ("mouse Run");} @Overridepublic void Close () {System.out.println ("mouse Close");} }
Keyboard for USB:
Package Cn.fanse;public class Keybyusb implements usb{@Overridepublic Void Open () {System.out.println ("key Run");} @Overridepublic void Close () {System.out.println ("key Close");} }
Main function class:
Package Cn.fanse;import Java.io.file;import Java.io.fileinputstream;import java.io.ioexception;import Java.util.properties;public class Notbookdemmo {/** * @param args * @throws Exception */public static void Main (String [] args) throws Exception { Notebook book = new Notebook (); Book.run (); File config = new file ("Usb.properties"); if (!config.exists ()) { config.createnewfile (); } FileInputStream fis = new FileInputStream (config); Properties Prop =new Properties (); Prop.load (FIS); for (int x =1;x<=prop.size (); x + +) {String className = prop.getproperty ("USB" +x); Class Clazz=class.forname (className); USB USB = (USB) clazz.newinstance (); BOOK.USEUSB (USB);}}}
Configuration file:
Write the following in the configuration file usb.properties:
Usb1=cn.fanse.mousebyusb
Usb2=cn.fanse.keybyusb
Results:
Book Run
Mouse Run
Mouse Close
Key Run
Key Close
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
An example of Reflection laptop USB Interface Example