繁体   English   中英

数组列表<Integer> getDeclaredMethod(&quot;add&quot;, Integer.class) 抛出 NoSuchMethodException

[英]ArrayList<Integer> getDeclaredMethod("add", Integer.class) throws NoSuchMethodException

ArrayList<Integer> intList = new ArrayList<>();
intList.getClass().getDeclaredMethod("add", Integer.class); // throws NoSuchMethodException

//where as
intList.getClass().getDeclaredMethod("add", Object.class); // is just fine.

当然,在这种情况下,我希望第一个工作。 或者至少弄清楚如何从泛型类型参数获取方法。

当循环声明的方法时, add方法明确表示 Object:

public boolean java.util.ArrayList.add(java.lang.Object)

我的猜测是我错过了一些简单的东西。

因为声明是add(E e) ArrayList<E>是泛型类型,它不知道实际类型。

参见例如这个

所以它最能猜到E的类型是Object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM