The Getclasses () method of the 1.Class object gets all the common inner classes in the class, as well as the inner classes inherited from the parent class, the parent interface. The Getinterfaces () method returns all interfaces inherited by the class.
ImportJavax.print.attribute.standard.PrinterInfo;Interfacehasbatteries{}Interfacewaterproof{}Interfaceshootsthings{}classtoy{Toy () {} Toy (inti) {}}classFancytoyextendsToyImplementshasbatteries,waterproof,shootsthings{ PublicFancytoy () {//TODO Auto-generated constructor stub Super(1); }} Public classToytest { Public Static voidMain (string[] args) {Class Class1=NULL; Try{Class1=class1.forname ("Fancytoy"); }Catch(ClassNotFoundException e) {//Todo:handle Exception} printinfo (Class1); Class[] Faces=class1.getinterfaces (); for(inti=0;i<faces.length;i++) {printinfo (faces[i]); } Class Cy=Class1.getsuperclass (); Object CObject=NULL; Try{cObject=cy.newinstance (); }Catch(Exception e) {//Todo:handle Exception} printinfo (Cobject.getclass ()); } Static voidPrintinfo (Class cc) {System.out.println ("Class Name:" +cc.getname () + "is interface?" +cc.isinterface ()); }}
The forname () of a 2.Class object uses the class name passed when the classname parameter is the full name, not just the class name, such as the package name. Class name, java.lang.String.
PackageEleven;Importjava.lang.reflect.*;classPig { PublicPig () {//TODO Auto-generated constructor stub } Public voidT () {} Public voidA () {} Public voidMa () {}} Public classShowmethods { Public Static voidMain (string[] args) {Try { //Class Class1 = Class.forName ("eleven. Pig ");Class Class1 = Class.forName ("java.lang.String"); Method[] Methods=Class1.getmethods (); Constructor[] Constructors=class1.getconstructors (); for(inti = 0; i < methods.length; i++) {System.out.println (methods[i].tostring ()); } for(inti = 0; i < constructors.length; i++) {System.out.println (constructors[i].tostring ()); } } Catch(ClassNotFoundException e) {//Todo:handle ExceptionSystem.out.println ("No such class:" +e); } }}
Java Learning notes-11. Run-Time type identification