日期:2014-05-20 浏览次数:20770 次
背景:在项目中有一堆javabean,要用在界面上画一堆tableview,要对这一堆tableview进行排序,因此要写一堆sorter类(假如为每个tableview都写一个的话),工作量比较大。
??????? 思考:其实这些sorter类都是差不多的,只是调用的javabean不同,方法名称不同,实现的原理,没有什么不同,那么该如何使用一个sorter类就可对所有的tableview进行排序呢?
??????? 思考结果:也许可以用Class类与Method类,也就是什么reflect机制。
??????? 困难:从来没用过reflect机制。
?????? 没有过,没关系。JDK5.0文档与google是我最好的老师。
?????? 下面就是我的成果了:
?????? 第一步,先建个javabean:
public?class?Model?{
????private?String?id;
????private?String?name;
????private?String?description;
????
????public?Model()?{
????????id?=?"id";
????????name?=?"name";
????????description?=?"description";
????}
????public?String?getId()?{
????????return?id;
????}
????public?void?setId(String?id)?{
????????this.id?=?id;
????}
????public?String?getName()?{
????????return?name;
????}
????public?void?setName(String?name)?{
????????this.name?=?name;
????}
????public?String?getDescription()?{
????????return?description;
????}
????public?void?setDescription(String?description)?{
????????this.description?=?description;
????}
}
??????? 第二步:
import?java.lang.reflect.InvocationTargetException;
import?java.lang.reflect.Method;
public?class?TestMethodReflect?{
????public?static?final?String?ID?=?"Id";
????public?static?final?String?NAME?=?"Name";
????public?static?final?String?DESCRIPTION?=?"Description";
????//方法名集合
????public?static?final?String[]?ALL?=?{?ID,?NAME,?DESCRIPTION?};
????//这是测试数据
????public?static?final?String[]?MODELDATA?=?{?"1",?"Gavin",
????????????"this?is?model's?test?data"?};
????/**
?????*?@param?args
?????*/
????public?static?void?main(String[]?args)?{
????????try?{
????????????//获得Model类
????????????Class?model?=?Class.forName("test.Model");
????????????//获得Model类的实例
????????????Object?object?=?model.newInstance();
????????????for?(int?i?=?0;?i?<?ALL.length;?i++)?{
????????????????//获得Model类的set方法,参数为String类型
????????????????Method?setMethod?=?model.getMethod("set"?+?ALL[i],?String.class);
????????????????//调用set方法
????????????????setMethod.invoke(object,?MODELDATA[i]);
????????????????//获得Model类的get方法,无参数
????????????????Method?getMethod?=?model.getMethod("get"?+?ALL[i],?null);
????????????????//调用get方法,并输出数据
????????????????System.out.println(getMethod.invoke(object,?null));
????????????}
????????}?catch?(ClassNotFoundException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}?catch?(SecurityException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}?catch?(NoSuchMethodException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}?catch?(IllegalArgumentException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}?catch?(IllegalAccessException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}?catch?(InvocationTargetException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}?catch?(InstantiationException?e)?{
????????????//?TODO?Auto-generated?catch?block
????????????e.printStackTrace();
????????}
????}
}
??????? ok,大功告成!
??????? 成功后的思考:我只要在一个sorter类里添加一个classname的参数,添加一个methodName的参数(类型为String[]),用一个for循环就有可能用一个sorter类为每个tableview加入排序的功能。