Java reflection Gets the property values of a field and assigns a value to a field, among other methods

Source: Internet
Author: User

1. Get property values for a class

 1  /*   2  cjjssetdto obj;  3  */  String filedname = "Cj" + (I+1 5  6  Cl  ass<?> cl = Obj.getclass ();  7  Method me = Cl.getdeclaredmethod ("Get" +filedname);                         8  9  String value = (string) me.invoke (obj); 

In the cjjssetdto, known as cj1,cj2 ... The field. Because the columns are not fixed, you need to use reflection when you get the values. The values of fields such as CJ1,CJ2 are taken through a loop traversal.

2. Set the value of a property

1  /** Set Value */ 2                        String filedName1 = "CJ" + (i+1); 3                        Field name = Xs.getclass (). Getdeclaredfield (filedname1+ "");   4                         Name.setaccessible (true);    5                         Name.set (xs,double.valueof (value));   6                         Name.setaccessible (false);  

Set the values of fields such as cj1,cj2 by looping through loops.

Among them, Field is Java.lang.reflect.Field;

Method is Java.lang.reflect.Method;

Java reflection Gets the property values of a field and assigns a value to a field, among other methods

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.