如何交互ArrayList <Class <?扩展IMyInterface >>

Pro*_*Rev 2 java iteration interface arraylist

我有一个ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();.当我尝试迭代它时,我得到:

Incopatible types:  
Required: java.lang.Class <? extends IMyInterface>  
Found: IMyInterface  
Run Code Online (Sandbox Code Playgroud)

我的迭代

for (IMyInterface iMyInterface : IMyInterface.getMyPluggables()) {}
Run Code Online (Sandbox Code Playgroud)

红色代码警告突出显示(Android Studio)

Error:(35, 90) error: incompatible types: Class<? extends IMyInterface> cannot be converted to IMyInterface  
Run Code Online (Sandbox Code Playgroud)

我想要

ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();

for (Class<? extends IMyInterface> myClass : classes) {
    if (myClass instanceof IMyInterface) {
        View revPluggableViewLL = myClass.getMyInterfaceMethod();
    }
}
Run Code Online (Sandbox Code Playgroud)

错误

Inconvertible types; cannot cast 'java.lang.Class<capture<? extends com.myapp.IMyInterface>>' to 'com.myapp.IMyInterface'  
Run Code Online (Sandbox Code Playgroud)

我该怎么去迭代呢?

谢谢大家.

dav*_*xxx 5

您希望迭代实例,IMyInterface因为您要调用以下特定方法IMyInterface:

    View revPluggableViewLL = myClass.getMyInterfaceMethod();
Run Code Online (Sandbox Code Playgroud)

问题是,你宣布ListClass实例:

ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)

它不包含任何实例,IMyInterface只包含 Class实例.

为了满足您的需求,请声明以下列表IMyInterface:

List<IMyInterface> instances = new ArrayList<>();
Run Code Online (Sandbox Code Playgroud)

并以这种方式使用它:

for (IMyInterface myInterface : instances ) {
   View revPluggableViewLL = myInterface.getMyInterfaceMethod();   
}
Run Code Online (Sandbox Code Playgroud)

请注意,此检查不是必需的:

if (myClass instanceof IMyInterface) {
    View revPluggableViewLL = myClass.getMyInterfaceMethod();
}
Run Code Online (Sandbox Code Playgroud)

你操纵ListIMyInterface,所以的元素List是必然的情况IMyInterface.