Java Learning notes-11. Run-Time type identification

Source: Internet
Author: User
Tags stub

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.