Java 27-3 Reflection Gets the construction method by reflection and uses the

Source: Internet
Author: User

Class Constructor<t>: Provides information about a single construction method of a class and access to it.

Get the construction method by means of reflection and use PS: First ignore generics

  A.1: Gets an array of constructor methods:

Public constructor<?>[] GetConstructors (): Get all common constructor methods

Public constructor<?>[] Getdeclaredconstructors (): Get all construction methods

  A.2: Gets a single constructor method (for non-private construction methods)

Public constructor<t> getconstructor (class<?> ... parametertypes)

Parameters are: The number of construction parameters you want to get, and the class byte-code file object of the data type

  A.3: Gets a single constructor method (for private construction methods)

Public constructor<t> getdeclaredtconstructor (class<?> ... parametertypes)

  B.1: Initializing an instance of a construction method: (for non-private construction methods)

Public T newinstance (object ... initargs) Note: t represents a generic, object ... Initargs represents the specified parameter you want to instantiate

Use the constructor method represented by this constructor object to create a new instance of the Declaration class for the constructed method, and initialize the instance with the specified initialization parameters.

  B.2: Initializing an instance of a construction method: (for private construction methods)   

First use: public void setaccessible(Boolean flag): The value of flag is true to indicate that the reflected object should cancel the Java language access check when it is used.

Re-use: Public T newinstance (Object ... initargs)

Code:

  A:public constructor<?>[] GetConstructors (): Get all public constructor methods

1         constructor[] C1 = c.getconstructors (); 2         SYSTEM.OUT.PRINTLN (C1); // returns the address value, proving that the array object is obtained 3         // iterate over this array 4          for (Constructor ct:c1) {5            System.out.println (CT); 6         }  // get all the announcement construction methods in the person class

  B:public constructor<?>[] getdeclaredconstructors (): Get all construction methods

1         // b:public constructor<?>[] getdeclaredconstructors (): Get all construction methods 2          constructor[] C1 = c.getdeclaredconstructors (); 3          // iterating through an array 4           for (Constructor ct:c1) {5             System.out.println (CT); 6          // get all the constructor methods for the person class

  

  C:public constructor<t> getconstructor (class<?> .... parametertypes): Gets the specified construction method

Get the parameterless construction method: 

1         Constructor C1 = c.getdeclaredconstructor (); 2         SYSTEM.OUT.PRINTLN (C1);

Get the parameter construction method:

1         // gets the specified constructor method, which requires a list of parameters for the method 2         Constructor C1 = C.getdeclaredconstructor (String.  Class,int. class, String. class ); 3         SYSTEM.OUT.PRINTLN (C1);

  D:public t newinstance (Object ... initargs) Note: t represents a generic, object ... Initargs represents the specified parameter you want to instantiate

    Use the constructor method represented by this constructor object to create a new instance of the Declaration class for the constructed method, and initialize the instance with the specified initialization parameters.

That is, initializing the object

Get the common parameter construction method by reflection and use:

1         //gets the specified constructor method, which requires a list of parameters for the method2Constructor C1 = C.getdeclaredconstructor (String.class,int.class, String.class);3 System.out.println (C1);4         5         //To create an object by using the parameter construction method object6         //Public T newinstance (Object ... initargs):7         //Generic , no generics in front, use Objcet first8Object B = c1.newinstance ("Zhang San", 13, "Hometown");9System.out.println (b);//Person [Name= Zhang San, age=13, address= hometown]

Get the private parameter construction method by reflection and use:

1         //gets the specified private constructor method2Constructor C1 = C.getdeclaredconstructor (String.class);3         4         //Public void Setaccessible (Boolean flag):5         //A value of flag of TRUE indicates that the reflected object should cancel the Java language access check when it is used. 6C1.setaccessible (true);7         8         //Public T newinstance (Object ... initargs)9Object ob = C1.newinstance ("Zhang San");TenSYSTEM.OUT.PRINTLN (OB);

The last two codes, equivalent to the previous: only through reflection, do not need to import the corresponding class

1  Public int Age , String address) 2  3  New Person ("Zhang San", 13, "Hometown"); 4  SYSTEM.OUT.PRINTLN (P);

And

1  Private Person (String name) {} 2 3  New Person ("Wind Qing"); 4  SYSTEM.OUT.PRINTLN (P);

Java 27-3 Reflection Gets the construction method by reflection and uses the

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.