getMethod()とinvoke()メソッドの適用
2527 ワード
簡単なニーズの実装:プログラムでクラスのメソッドを呼び出し、結果println()を返します.実装方法:
MethodTest.java
TestVO.java
http://www.blogjava.net/vesung/archive/2008/01/24/177447.html
MethodTest.java
import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Method;
import java.lang.reflect.InvocationTargetException;
/**
* :
*
* @author wangjing
* Copyright: Copyright (c) 2006-2008 by vesung.cn.
* Date: 2008-1-23 14:13:26
*/
public class MethodTest{
TestVO vo = new TestVO();
/**
* 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 = new MethodTest();
m.setUp();
// ()
String methodName = "getInvoiceSortCode";
// method
//getMethod , ,
// ,
Method method = m.vo.getClass().getMethod(methodName,new Class[0]);
// ,
//
//
System.out.println(methodName+"():"+method.invoke(m.vo,new Object[0]));
}
}
TestVO.java
import java.util.List;
/**
* :
*
* @author wangjing
* Copyright: Copyright (c) 2006-2008 by vesung.cn.
* Date: 2008-1-23 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;
}
// getter(),setter()
//
}
http://www.blogjava.net/vesung/archive/2008/01/24/177447.html