Reflection in Java and Java reflection

Source: Internet
Author: User

Reflection in Java and Java reflection

Reflection, I have heard of it before, and I agree that if I want to write what kind of class is required, I will not be able to. Why should I explore other elements in human beings? So I have never learned anything about reflection, until a few days ago, you can see the quick slider image for modifying ListView, and you can see that reflection is needed.

I looked at reflection yesterday.

Concepts first

Reflection is a class that is known only when a runtime is loaded. It is informed of its complete structure and generates an object corpse, or sets or calls methods for its variables.

In other wordsUnderstanding classAndModify class

Understanding class is

Understanding the class from the constructor, member variables, methods, modifier of each element

Test class:

package test;public class MyObject {public int a ,b;public MyObject(){}public MyObject(int a, int b) {super();this.a = a;this.b = b;}public int sum(){return a+b;}public int minus(){return a-b;}public int multiply(){return a*b;}public int divide(){return a/b;}}

Class c = Class. forName ("test. MyObject ");

Slave Constructor

Constructor [] constructors = c. getConstructors (); for (int I = 0; I <constructors. length; I ++) {System. out. println ("------ method + I +" ----- >>> "+ constructors [I]. toString (); System. out. println ("-----> method name:" + constructors [I]. getName (); System. out. println ("-----> modifier:" + constructors [I]. getModifiers (); Class p [] = constructors [I]. getParameterTypes (); for (int j = 0; j <p. length; j ++) {System. out. println ("parameter j:" + p [j]) ;}}
Member variables

Field [] fields = c. getDeclaredFields (); for (int I = 0; I <fields. length; I ++) {Field f = fields [I]; System. out. println ("-------> variable name:" + f. getName (); System. out. println ("-------> variable type:" + f. getModifiers (); System. out. println ("-----> modifier:" + f. getType ());}

Method:

Method m [] = c. getDeclaredMethods (); for (int I = 0; I <m. length; I ++) {System. out. println ("------" + I + "method ----- >>>" + m [I]. toString (); System. out. println ("-----> method name:" + m [I]. getName (); System. out. println ("-----> modifier:" + m [I]. getModifiers (); System. out. println ("------> return value:" + m [I]. getReturnType (); Class p [] = m [I]. getParameterTypes (); for (int j = 0; j <p. length; j ++) {System. out. println ("parameter j:" + p [j]) ;}}
Understand the class from the above aspects

The following describes how to apply a class through reflection.

// Create the Class object Class [] p = new Class [2]; p [0] = int. class; p [1] = int. class; Constructor constructor = c. getConstructor (p); Object object = constructor. newInstance (34, 90); // modify the member variable Field fielda = c. getField ("a"); System. out. println ("-----> before change:" + fielda. get (object); fielda. setInt (object, 190); System. out. println ("-----> changed:" + fielda. get (object); // call the Method of the class Method method1 = c. getMethod ("sum"); Object sum = method1.invoke (object); System. out. println ("---> plus =" + sum );









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.