Transferred from:Http://www.blogjava.net/vesung/archive/2008/01/24/177447.html
Implementation of a simple requirement:
InProgramAnd returns the result println.
Implementation Method:
Methodtest. Java
Import Java. util. arraylist;
Import Java. util. List;
Import Java. Lang. Reflect. method;
Import Java. Lang. Reflect. invocationtargetexception;
/**
* Function description:
*
*@ Author<A href = 'mailto: vesung@gmail.com '> Wangjing </a>
* Copyright: Copyright (c) 2006-2008 by vesung.cn.
* Date: 14:13:26
*/
Public Class Methodtest {
Testvo= NewTestvo ();
/**
* Initialize Vo
*/
Protected Void Setup () {
VO. setinvoicesortcode ( " Invoicesortcode " );
VO. setinvoicesortname ( " Invoicesortname " );
VO. setoperaddress ( " Operaddress " );
VO. setstockstatus ( " Stockstatus " );
VO. setstockstatuscode ( " Stockstatuscode " );
List list = New Arraylist ();
List. Add ( " Stockstatuslist " );
VO. setstockstatuslist (list );
VO. settaxpayername ( " Taxpayername " );
VO. settaxregcode ( " Taxregcode " );
}
Public Static Void Main (string [] ARGs) Throws Illegalaccessexception, nosuchmethodexception, invocationtargetexception {
Methodtest m= NewMethodtest ();
M. Setup ();
//The method name to be executed is not ()
String methodname= "Getinvoicesortcode";
// Obtain the corresponding method object first.
// The first parameter of getmethod is the method name, and the second parameter is the parameter type of the method,
// Because there are different parameters with the same method name, a method can be uniquely identified only when the method name and parameter type are specified at the same time.
Method = M. VO. getclass (). getmethod (methodname, New Class [ 0 ]);
// Next we should execute this method and explain the parameters.
// The first parameter is the object that calls this method.
// The second parameter is the specific parameter for executing this method.
System. Out. println (methodname + " (): " + Method. Invoke (M. VO, New Object [ 0 ]);
}
}
Testvo. Java
Import Java. util. List;
/**
* Function description:
*
*@ Author<A href = 'mailto: vesung@gmail.com '> Wangjing </a>
* Copyright: Copyright (c) 2006-2008 by vesung.cn.
* Date: 14:17:14
*/
Public Class Testvo {
Private String taxregcode;
Private String stockstatuscode;
Private String stockstatus;
Private List stockstatuslist;
Private String taxpayername;
Private String operaddress;
Private String invoicesortcode;
Private String invoicesortname;
Public String pub;
Public String getinvoicesortcode (){
Return invoicesortcode;
}
//Omit getter (), setter ()
//
}