Static loading:
Package COM.IMOOC. Loading class;
public class Office_static {public
static void Main (string[] args) {
//new Create object, is statically loaded class, need to load all possible classes at compile time
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 to do, even if you do not use Excel to load
Dynamic loading:
1, Interface officeable:
Package COM.IMOOC. Loading class;
Public interface Officeable {public
void Start ();
}
2, Word implementation interface:
Package COM.IMOOC. Loading class;
public class Word implements officeable{public
void Start () {
System.out.println ("Word start");
}
3, Excel Implementation interface:
Package COM.IMOOC. Loading class;
public class Excel implements officeable{public
void Start () {
System.out.println ("Excel start");
}
4. Main method
Package COM.IMOOC. Loading class;
public class Officebetter {
/**
* @param args
*
/public static void main (string[] args) {
try {
/ /dynamic load class, at run time loaded class
C = Class.forName (Args[0]);//Enter COM.IMOOC in Run configuration. Load class. Excel
//Through the class type, create the class object ( First convert to Word and Excel's Common interface officeable)
officeable oa = (officeable) c.newinstance ();
Oa.start ();
The following two are not recommended because you are not sure whether to load Word or Excel, or to turn
// Word word = (word) c.newinstance (); Word.start (); Excel Excel = (Excel) c.newinstance (); Excel.start ();
} catch (Exception e) {
e.printstacktrace ();}}}
The above is a small series for everyone to bring the static load of Java reflection and dynamic loading of the simple example of all the content, I hope to help you, a lot of support cloud Habitat Community ~