Directly on the code, relatively simple people should be able to read:
Package com.bluezhang.reflect;/** * Create USB interface * Mouse keyboard etc all belong to USB device * */public interface USB {//Open usbpublic Void Open ();//Close Usbpub LIC void Close ();}
/** * */package com.bluezhang.reflect;/** * @author Bluezhang * */public class Moususb implements usb{@Overridepublic void Open () {//System.out.println ("Mouse open! ");} @Overridepublic void Close () {//System.out.println ("MOUs close!");}}
Package com.bluezhang.reflect;/** * @author Bluezhang * class to implement USB interface * */public class KEYUSB implements usb{@Overridepublic V OID Open () {//System.out.println ("Key open!");} @Overridepublic void Close () {//System.out.println ("Key close!");}}
/** * */package com.bluezhang.reflect;/** * @author Bluezhang * */public class Notpead {//computer running public void run () {System . OUT.PRINTLN ("computer Run");} The computer uses usbpublic void use (USB USB) {if (usb!=null) {usb.open ();//usb.close ();/}}}
/** * */package com.bluezhang.reflect;import java.io.fileinputstream;import Java.io.filenotfoundexception;import java.util.properties;/** * @author Administrator * */public class Test {/** * @param args * Notebook can run notebook can use mouse Notebook can use keyboard * @throws FileNotFoundException * @throws classnotfoundexception * @throws illegalaccessexception * @throws I Nstantiationexception * */public static void Main (string[] args) throws FileNotFoundException, ClassNotFoundException, I Nstantiationexception, illegalaccessexception {//notpead not = new Notpead ();//Call your own Run method Not.run ();///* * According to our usage habits that is Create two objects first and then call the appropriate method * * * KEYUSB KEYUSB = new Keyusb (); Notebook.use (KEYUSB); * * Mouseusb MOUSEUSB = new Mouseusb (); Notebook.use (MOUSEUSB); * But we found that if there was always a new device, then we always needed a new one to create a new object * * And then call the method inside to make the scalability of our program more difficult * * So use the reflection mechanism if there is a new device to join then only need to change our configuration file does not have to change our * * Te St Code. This improves the extensibility of our code *///Create properties Object Properties Properties = new properties ();//Create input stream FileInputStream FIS = new FileInputStream ("Config\\config.properties ");//import inputstreamfor (int i= 0;i<properties.size (); i++) {String value = Properties.getproperty ("USB" +i);//class<?> Claz = Class.forName (value);//object obj = Claz.newinstance ();//usb USB = (USB) obj;// Usb.open ();//usb.close ();/}}}
Create a configuration file in your own project config.properties is stored in a key-value pair that is preceded by the corresponding package name Class name
Configuration file:
Usb1=com.bluezhang.reflect.keyusb
Usb2=com.bluezhang.reflectmoususb
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Simple principle to introduce the reflection of Java programming