已经一个object中有一个名为getXXX的函数,如何执行这个函数?
应该用java的反射机制能解决,不知具体如何实现?

解决方案 »

  1.   

    刚看文档发现了getMethods及invoke函数应该可以实现,你测试一下.
    classe.getMethods()[0].invoke(...)
      

  2.   

    import java.beans.BeanInfo;
    import java.beans.IntrospectionException;
    import java.beans.Introspector;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;public class BeanTest{    public static void main(String[] args) throws IntrospectionException,
                IllegalArgumentException, IllegalAccessException,
                InvocationTargetException {        Haha haha = new Haha();
            
            BeanInfo bean = Introspector.getBeanInfo(haha.getClass());   
            PropertyDescriptor[] properties = bean.getPropertyDescriptors();
            
            for(PropertyDescriptor property : properties) {            
                // 获得属性的set方法
                Method methodWrite = property.getWriteMethod();
                // 获得属性的get方法
                Method methodRead = property.getReadMethod();            if(methodWrite == null || methodRead == null) {
                    continue;
                }            methodWrite.invoke(haha, "abc");            Object obj = methodRead.invoke(haha);
                System.out.println(property.getDisplayName() + " --> " + (String) obj);
            }
        }
    }class Haha {
        private String str1;
        private String str2;
        public String getStr1() {
            return str1;
        }
        public void setStr1(String str1) {
            this.str1 = str1;
        }
        public String getStr2() {
            return str2;
        }
        public void setStr2(String str2) {
            this.str2 = str2;
        }
    }
      

  3.   

    或者也可以改成下面这样的,方便一些,注意不要直接通过getXxx方法直接调用,应该通过属性
    来找到这个属性的get方法后再行调用。import java.beans.IntrospectionException;
    import java.beans.PropertyDescriptor;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;public class BeanTest {    public static void main(String[] args) throws IntrospectionException,
                IllegalArgumentException, IllegalAccessException,
                InvocationTargetException {
            Haha haha = new Haha();
            
            Class clazz = haha.getClass();
            Field[] fields = clazz.getDeclaredFields(); 
            
            for(Field field : fields) {
                PropertyDescriptor property = new PropertyDescriptor(field.getName(), clazz);
                // 获得属性的set方法
                Method methodWrite = property.getWriteMethod();
                // 获得属性的get方法
                Method methodRead = property.getReadMethod();            methodWrite.invoke(haha, "abc");            Object obj = methodRead.invoke(haha);
                System.out.println(property.getDisplayName() + " --> " + (String) obj);
            }
        }
    }
      

  4.   

    啊,还是带参数的get方法啊,我还以为是以普通的JavaBean呢,那只能直接通过方法名和参数来查找了,重写一个:import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;public class TimeZone {    public static void main(String[] args) throws SecurityException,
                NoSuchMethodException, IllegalArgumentException,
                IllegalAccessException, InvocationTargetException {
            Haha haha = new Haha();
            Method method = haha.getClass().getMethod("getName", new Class[] { String.class, String.class });
            String result = (String) method.invoke(haha, new Object[] { "Bill", "Gates" });
            System.out.println(result);
        }
    }class Haha {
        public String getName(String firstname, String lastname) {
            return firstname + " " + lastname;
        }
    }