Black Horse programmer _ reflection

Source: Internet
Author: User

Reflection

???? Under normal circumstances, we must know the complete path of a class before instantiating the object, but in Java, we can also find the information of the class where it is located through an object, this is actually the function of the class.

PackageReflect;

ClassX {

????

}

PublicClassGetclassdemo01 {

????PublicStaticVoidMain (string [] ARGs ){

???????? X =NewX ();

???????? System.Out. Println (X. getclass (). getname ());

????}

}

We can see that all the operations at this time are reversed, Which is reflection.

Class

???? The class itself represents the class itself. Through the class, you can obtain the complete structure of a class, including the method definition and attribute definition in this class.

?

No constructor is found in the JDK documentation, so such constructor is private.

Three methods for instantiating class objects

????? First: Use the forname () method

????? Type 2: pass class

????? Third: object. getclass ()

PackageReflect;

ClassX {

????

}

PublicClassGetclassdemo01 {

????PublicStaticVoidMain (string [] ARGs ){

???????? Class <?> C1 =Null;

???????? Class <?> C2 =Null;

???????? Class <?> C3 =Null;

????????Try{

???????????? // Instantiate the class object in the first way. This method is also the most commonly used form.

???????????? C1 = Class.Forname("Reflect. X ");

???????????? // Instantiate the class object through the disallow in the object class

???????????? C2 =NewX (). getclass ();

???????????? // Instantiate a class through class. Cass

???????????? C3 = x.Class;

???????????? System.Out. Println ("Class Name:" + c1.getname ());

???????????? System.Out. Println ("Class Name:" + c2.getname ());

???????????? System.Out. Println ("Class Name:" + c3.getname ());

????????}Catch(Classnotfoundexception e ){

???????????? //TodoAuto-generated Catch Block

???????????? E. printstacktrace ();

????????}

????}

}

?

?

?

?

?

?

?

?

?

?

?

?

?

?

Black Horse programmer _ reflection

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.