Static loading:
Package COM.IMOOC. Load class, public class Office_static {public static void main (string[] args) {//new creates an object, is a static load class, At compile time you need to load all the possible classes if ("word". Equals (Args[0])) {Word w = new Word (); W.start ();} if ("Excel". Equals (Args[0])) {Excel e = new Excel (); E.start ();}}}
This program must be compiled with Word and Excel two classes exist before the line, even if you do not use Excel to load
Dynamic loading:
1, Interface officeable:
Package COM.IMOOC. Load class; public interface officeable {public void Start ();}
2, Word implementation interface:
Package COM.IMOOC. Load class, public class Word implements Officeable{public void Start () {System.out.println ("Word start");}}
3, Excel Implementation interface:
Package COM.IMOOC. Load class, public class Excel implements Officeable{public void Start () {System.out.println ("Excel start");}}
4. Main method
Package COM.IMOOC. Load class, public class Officebetter {/** * @param args */public static void main (string[] args) {try {//Dynamically load class, Load Class C = Class.forName (Args[0]) at run time;//Enter COM.IMOOC in the Run configuration. Load class. excel//creates the class object through the class type ( Convert first to Word and Excel Common interface officeable) officeable OA = (officeable) c.newinstance (); Oa.start ();//not recommended for the following two kinds, Because you are not sure whether to load Word or Excel, go//word Word = (word) c.newinstance ();//word.start ();//excel Excel = (Excel) c.newinstance ();// Excel.start ();} catch (Exception e) {e.printstacktrace ();}}}
An example of static loading and dynamic loading of Java reflection