Application of getmethod () and invoke () methods in Java

Source: Internet
Author: User

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 ()
//

}

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.