Java invoke 1 [dynamic call class method]

Source: Internet
Author: User

Java invoke 1 [dynamic call class method]

This example can be used flexibly when importing data. It is convenient and practical.

1. User. Java

package cvr.vo;public class User {private String username;private String password;private Integer value1,value2,value3,value4,value5,value6,value7,value8,value9,value10;public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public Integer getValue1() {return value1;}public void setValue1(Integer value1) {this.value1 = value1;}public Integer getValue10() {return value10;}public void setValue10(Integer value10) {this.value10 = value10;}public Integer getValue2() {return value2;}public void setValue2(Integer value2) {this.value2 = value2;}public Integer getValue3() {return value3;}public void setValue3(Integer value3) {this.value3 = value3;}public Integer getValue4() {return value4;}public void setValue4(Integer value4) {this.value4 = value4;}public Integer getValue5() {return value5;}public void setValue5(Integer value5) {this.value5 = value5;}public Integer getValue6() {return value6;}public void setValue6(Integer value6) {this.value6 = value6;}public Integer getValue7() {return value7;}public void setValue7(Integer value7) {this.value7 = value7;}public Integer getValue8() {return value8;}public void setValue8(Integer value8) {this.value8 = value8;}public Integer getValue9() {return value9;}public void setValue9(Integer value9) {this.value9 = value9;}}

2. Go. Java

Package CVR. go; import Java. lang. reflect. method; import Java. util. arraylist; import Java. util. list; import CVR. VO. user; public class go {public static void main (string [] ARGs) {int [] Vals = new int [] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; user user = go. setuservalues (Vals); List list = go. getuservalues (User); For (INT I = 0; I <list. size (); I ++) {Int J = I + 1; system. out. println ("value" + J + "=" + list. get (I) ;}} public static user setuservalues (INT [] Vals) {user = new user (); try {// get the Class Object Class C = user. getclass (); For (INT I = 1; I <= 10; I ++) {// Dynamically Retrieve the method name string methodname = "setvalue" + I; // set the parameter class [] ARGs = new class [1]; integer u = new INTEGER (3); ARGs [0] = u. getclass (); Method method = C. getmethod (methodname, argS); // call the method. invoke (user, Vals [I-1]);} catch (exception e) {e. printstacktrace ();} return user;} public static list getuservalues (User user) {list = new arraylist (); try {Class C = user. getclass (); For (INT I = 1; I <= 10; I ++) {string methodname = "getvalue" + I; Method method = C. getmethod (methodname, null); integer S = (integer) method. invoke (user, null); list. add (s) ;}} catch (exception e) {e. printstacktrace ();} return list ;}}

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.