我试图理解 Java 反射,但在使用非整数 setter 方法时遇到困难。
例如,如何解析下面的“getDeclaredMethod()”调用?
import java.lang.reflect.*;
class Target {
String value;
public Target() { this.value = new String("."); }
public void setValue(String value) { this.value = value; }
public String getValue() { return this.value; }
}
class ReflectionTest {
public static void main(String args[]) {
try {
Class myTarget = Class.forName("Target");
Method myMethod;
myMethod = myTarget.getDeclaredMethod("getValue"); // Works!
System.out.println("Method Name: " + myMethod.toString());
Class params[] = new Class[1];
//params[0] = String.TYPE; // ?? What is the appropriate Class TYPE?
myMethod = myTarget.getDeclaredMethod("setValue", params); // ? Help ?
System.out.println("Method Name: " + myMethod.toString());
} catch (Exception e) {
System.out.println("ERROR");
}
}
}
params[0] = String.class;
Using class
on String
将返回Class<?>
与相关的String
class.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)