【发布时间】:2018-04-14 15:38:26
【问题描述】:
我有一个ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();。当我尝试迭代它时,我得到:
Incopatible types:
Required: java.lang.Class <? extends IMyInterface>
Found: IMyInterface
我的迭代
for (IMyInterface iMyInterface : IMyInterface.getMyPluggables()) {}
红色代码警告高亮(Android Studio)
Error:(35, 90) error: incompatible types: Class<? extends IMyInterface> cannot be converted to IMyInterface
我愿意
ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();
for (Class<? extends IMyInterface> myClass : classes) {
if (myClass instanceof IMyInterface) {
View revPluggableViewLL = myClass.getMyInterfaceMethod();
}
}
错误
Inconvertible types; cannot cast 'java.lang.Class<capture<? extends com.myapp.IMyInterface>>' to 'com.myapp.IMyInterface'
我该如何迭代它?
提前谢谢大家。
【问题讨论】:
-
您尝试迭代
ArrayList<Class<? extends IMyInterface>>而不是ArrayList<IMyInterface>。顺便说一句,这不是您想要的...Class是表示该类型的实例,而不是该类型本身的实例。所以instanceof IMyInterface永远不会是真的。
标签: java arraylist interface iteration