Java Reflection--construction method

Source: Internet
Author: User

We all know that the reflection technology is very important in Java, a technical point, because Java a lot of frameworks are written based on reflection, not to mention that the spring framework of the IOC is based on reflection implementation. So what is reflection? The Java reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any object, can invoke its arbitrary methods and properties; This dynamic acquisition of information and the ability to dynamically invoke object methods is called the reflection mechanism of the Java language. To be blunt, reflection is the load class and reflects the implementation of the various components of the class.

So how do we load a class? This can be done in several ways.

 Public void throws classnotfoundexception {        //  get Byte code class, can take three ways         //  Equivalent to loading the Person.class file into the in-memory        Class clazz = Class.forName ("Com.day09.Person");         New Person (). GetClass ();         = person. class ;    }

Java is an object-oriented language, and Java creates objects generally by new+ constructs the object, so let's first look at how to reflect the class's construction method. At the same time, because of the Java method overloading mechanism, so the construction method also has many properties, the following example will demonstrate several of the most common construction methods, and use reflection to use them to create objects.

1. Define a simple person class

 Packagecom.day09;Importjava.util.List; Public classPerson { PublicPerson () {System.out.println ("Person"); }     PublicPerson (String name) {System.out.println ("Person Name"); }     PublicPerson (String name,intpassword) {System.out.println ("Person name password"); }    PrivatePerson (List list) {System.out.println ("Person List"); }}

2. Use JUnit to demonstrate the demo test

 Packagecom.day09;Importjava.util.List;ImportJava.lang.reflect.Constructor;Importjava.util.ArrayList;Importorg.junit.Test;/*** Radiation is the loading class, and the anatomy of the components of the class's constituent classes: 1. How to: Create an object 2. Attribute fields: Encapsulating data 3. Attribute method: Encapsulation function * *@authorAdministrator **/ Public classReflectdemo {@Test Public voidTestloadclass ()throwsClassNotFoundException {//Get the Byte code class, you can use three ways//equivalent to loading the Person.class file into memoryClass clazz = Class.forName ("Com.day09.Person"); Class CLAZZ1=NewPerson (). GetClass (); Class clazz2= person.class; }    /*** Public person () reflection non-parametric construction method * *@throwsException*/@Test Public voidReflectconstructnoparameter ()throwsException {//loading the person class into memoryClass<?> clazz = Class.forName ("Com.day09.Person"); //Pass in parameters to get the corresponding constructorconstructor<?> Personcon = Clazz.getconstructor (NULL); //incoming parameters start creating objectsPerson P = (person) personcon.newinstance (NULL); }    /*** Reflection with a parameter construction method construction method public person (String name) * *@throwsException*/@Test Public voidReflectconstructoneparameter ()throwsException {//loading the person class into memoryClass<?> clazz = Class.forName ("Com.day09.Person"); //Pass in the parameter, get the corresponding constructor, where the face to pass a parameter type, to get the corresponding parameter construction methodconstructor<?> Personcon = Clazz.getconstructor (String.class); //incoming parameters start creating objectsPerson P = (person) personcon.newinstance ("Zhangsan"); }    /*** Reflection with Multiple parameter construction method construction method person (String name, int password) * *@throwsException*/@Test Public voidReflectconstructmanyparameter ()throwsException {//loading the person class into memoryClass<?> clazz = Class.forName ("Com.day09.Person"); //Pass in the parameter, get the corresponding constructor, where the face to pass a parameter type, to get the corresponding parameter construction methodconstructor<?> Personcon = Clazz.getconstructor (String.class,int.class); //incoming parameters start creating objectsPerson P = (person) personcon.newinstance ("Zhangsan", 23); }    /*** Reflection Private constructor Method construction method Private person (list list) * *@throwsException*/@Test Public voidReflectprivateconstruct ()throwsException {//loading the person class into memoryClass<?> clazz = Class.forName ("Com.day09.Person"); //Pass in the parameter, get the corresponding constructor, where the face to pass a parameter type, to get the corresponding parameter construction method//This is because the constructor method is private, so you can use Getdeclaredconstructor to get the construction method object.constructor<?> Personcon = Clazz.getdeclaredconstructor (List.class); //to use a private property member, you also need to release the permission, set to visiblePersoncon.setaccessible (true); //incoming parameters start creating objectsPerson P = (person) personcon.newinstance (NewArrayList ()); }    /*** With class, you can create objects directly from a parameterless construction method *@throwsException*/@Test Public voidTestnewobj ()throwsException {//loading the person class into memoryClass<?> clazz = Class.forName ("Com.day09.Person"); Person P=(person) clazz.newinstance (); }}

At this point, we have a number of common through the reflection of the construction method, and create the object presentation finished, there are insufficient places, I hope you have a lot of advice!

Java Reflection--construction method

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.