0

我是java的新手。我无法理解 method.invoke 的使用。请你能给我更详细的信息。Java 代码:

import java.util.Comparator;
import java.lang.reflect.Method
public class TestComparator implements Comparator {

private Class classObject;
 public TestComparator(Class classObject,String methodName){
    this.classObject= classObject;
    this.methodName = methodName;
}

public int compare(Object object1, Object object2) {
    try{
        Method method = classObject.getMethod(("get"+methodName));
        String value1 = method.invoke(classObject.cast(object1)).toString();
        String value2 = method.invoke(classObject.cast(object2)).toString();
        return value2.compareTo(value1);
    }catch(Exception ex){
        //  ex.printStackTrace();
    }
    return 0;
}}

提前致谢。

4

0 回答 0