![](/img/trans.png)
[英]argument types sent to getDeclaredMethod(String, Class[]) in java reflection
[英]Java Reflection getDeclaredMethod() with Class Types
我正在嘗試理解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;
在String
上使用class
將返回與String
類關聯的Class<?>
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.