public class Test1<Type>
{
public Type getCompositeMessage(Type... strings)
{
Type val = (Type) "";
for (Type str : strings) {
val = (Type) ((String)val + (String)str);
}
return val;
}
}
检索方法:
try
{
Class<?> c = Class.forName("test1.Test1");
Method[] allMethods = c.getDeclaredMethods();
for (Method m : allMethods) {
String mname = m.getName();
System.out.println(mname);
}
Method m = c.getMethod("getCompositeMessage");
m.setAccessible(true);
Object o = m.invoke(c, "777777777777777777777777");
System.out.println(m);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
输出:
getCompositeMessage
java.lang.NoSuchMethodException: test1.Test1.getCompositeMessage()
at java.lang.Class.getMethod(Unknown Source)
at test1.Main.main(Main.java:25)
但是方法的名称是完全一样的!为什么我收到 NoSuchMethodException ?谢谢。