The following simplified code is available:
Preparation of Knowledge:
Student judy = new Student;
Class s1 = student.class;Class s2 = Judy.getclass ();
Class s3 = null;
s3 = class.forname ("com. Student ");Here s1, S2, S3 are all class type new object is static load class, at compile time need to load all the possible classes.
Examples of dynamic load classes:
1//Officeable.java
2Interfaceofficeable {
3
4 PublicvoidStart ();
5//System.out.println ("Word...stats
6}
1 // Word.java 2 class Implements officeable {34 Public void start () {5 System.out.println ("Word...starts ..."); 6 }78 }
1 //Officebetter.java2 classOfficebetter {3 4 Public Static voidMain (string[] args) {5 6 Try {7Class CType = Class.forName (Args[0]);//get class class type8Officeable OA =(officeable) ctype.newinstance ();9 Oa.start ();Ten One}Catch(Exception e) { A e.printstacktrace (); - } - } the}
The Officebetter class can be executed from the command line, with the execution arguments given, the class name of the class to be loaded dynamically.
The above finishing from MU class network of Java teaching Video http://www.imooc.com/video/3725/0, in this thank Mu class nets teacher!
Dynamic loading of class types and classes in Java