Simple principle to introduce the reflection of Java programming

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.