Java reflection: The dynamic call method according to the method name, solve the commodity dynamic property value problem.

Source: Internet
Author: User

public class goods{

Private String Goodsname;

Private String attr1;

Private String attr2;

Private String ATTR3;

Private String ATTR4;

.........

Private String Attr20;

Setter/getter method

}

In database table goods, the number of attributes of the product is known to be N (satisfies: N=5,ATTR1-ATTR5 has a value, Attr6-attr20 is empty). Ask how to remove the attribute values for each item:

Method One:

Remove the 20 attribute value

String attr1=goods.getattr1 ();

String attr2=goods.getattr2 ();

........

String Attr20=goods.getattr20 ();

Method Two:

Use reflection to take a value from a method name
Attr= Goods.getdeclaredmethod ("attr" +i);

The reflective knowledge involved, please consult other information, here only the dynamic product attribute value of the scheme.

Java reflection: The dynamic call method according to the method name, solve the commodity dynamic property value problem.

Related Article

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.