Look at the code first,
Package Reflect;//class Wordpublic Class Word implements officeable{public static void Main (string[] args) {}public void St Art () {System.out.println ("Word...start ...");} Public Word () {}}package Reflect;//class excelpublic Class Excel implements officeable{public static void Main (string[] Ar GS) {}public void start () {}public Excel () {}}package reflect;//officeable interface, implemented by classes such as Word and Excel interface officeable {public void Start ();} Test method Package Reflect;import Org.junit.test;public class Test01 {@Testpublic void Testor () throws ClassNotFoundException, instantiationexception, illegalaccessexception{//dynamic load class, loading//class a = Class.forName at run time (" Reflect. Word "); Class a = Class.forName ("reflect. Excel "); System.out.println ("A:" +a);//a:class reflect. EXCEL//creates the class object through the class type officeable OA = (officeable) a.newinstance (); Oa.start ();}}
Java interface benefits are obvious, such as class Word and class Excel are implemented Officeable interface, so for example to call the Word.start () method, when instantiating the word class, not necessarily cast to word, Instead, you can cast directly to the officeable type, and then call the start () method. The only job that needs to be done is to pass in the class to be used in the Class.forName (), and in this case the parameter passed in is "package." ClassName ", i.e." reflect. Word ", and if you need to instantiate Excel, you can just change the classname to Excel when you load the class.
Java Interface, Reflection