Re: [問題] 關於一個傳入參數 (自解 orz
※ 引述《InitialShuk (Shuk)》之銘言:
: public Method getMethod(String name,Class<?>... parameterTypes)
: ^^^^^^^^^^^^^^^^^^^^^^^^^^
: 說明:
: The parameterTypes parameter is an array of Class objects that identify the
: method's formal parameter types, in declared order. If parameterTypes is
: null, it is treated as if it were an empty array.
: 當name這個method沒有傳入參數時可以不用輸入 使用此getMethod得到需要的method
: 但是如果有傳入參數呢? ex:int number,String args[]......
: 我該怎麼輸入?
non-primitive type 幾乎都可以直接以 class literal 方式來寫,而 class
literal 寫法也蠻直覺。
primitive type 則要使用 Wrapper class 的 static field。
例如:
java.lang.Integer.TYPE 是一個 Class object,一個代表 primitive type: int
的 Class object。
http://java.sun.com/javase/6/docs/api/java/lang/Integer.html#TYPE
: -------------------------code----------------------
: URLClassLoader loader = new URLClassLoader(
: new URL[]{new URL("http://127.0.0.1:8080/")},null);
: Class<?> c = loader.loadClass("Helloworld");
Method mainMethod = c.getMethod("main", String[].class);
String[] argsForMain = new String[] {
"abc",
"def",
"...",
"etc",
};
mainMethod.invoke(null, (Object) argsForMain); // static method 不需要
// 指定 instance
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 218.173.129.63
※ 編輯: sbrhsieh 來自: 218.173.129.63 (12/05 17:53)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):