Static loading:
package com.imooc.load class; public class Office_Static {public static void main(String[] args) {//new Create an object, which is a static loading class. At the compilation time, all possible classes need to be loaded 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 have two classes: Word and Excel when compiling. Even if you cannot use Excel after judgment, it must be loaded.
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 {//Dynamic loading class, loading Class c = Class.forName(args[0]);//In the run configuration, enter com.imooc.load class.Excel//Create this class object through the class type (first convert to the common interface of Word and Excel) OfficeAble oa = (OfficeAble)c.newInstance();oa.start();//The following two are not recommended, because it is not sure whether to load Word or Excel, you must force it //Word word = (Word)c.newInstance();//word.start();//Excel excel = (Excel)c.newInstance();//excel.start();} catch (Exception e) {e.printStackTrace();}}}The above is the entire content of the simple examples of Java reflection: static loading and dynamic loading brought to you by the editor. I hope it will be helpful to you and support Wulin.com more~