I have an ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();
ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();
. When I try to iterate it, I get:
Incopatible types:
Required: java.lang.Class <? extends IMyInterface>
Found: IMyInterface
My iteration
for (IMyInterface iMyInterface : IMyInterface.getMyPluggables()) {}
Red code warning highlight (Android Studio)
Error:(35, 90) error: incompatible types: Class<? extends IMyInterface> cannot be converted to IMyInterface
I would like to
ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();
for (Class<? extends IMyInterface> myClass : classes) {
if (myClass instanceof IMyInterface) {
View revPluggableViewLL = myClass.getMyInterfaceMethod();
}
}
ERROR
Inconvertible types; cannot cast 'java.lang.Class<capture<? extends com.myapp.IMyInterface>>' to 'com.myapp.IMyInterface'
How can I go about iterating through it?
Thank you all in advance.
You want to iterate on instances of IMyInterface
as you want to invoke a specific method of IMyInterface
:
View revPluggableViewLL = myClass.getMyInterfaceMethod();
The problem is that you declared a List
of Class
instances :
ArrayList<Class<? extends IMyInterface>> classes = new ArrayList<>();
It doesn't contain any instance of IMyInterface
but only Class
instances.
To achieve your need, declare a list of IMyInterface
:
List<IMyInterface> instances = new ArrayList<>();
And use it in this way :
for (IMyInterface myInterface : instances ) {
View revPluggableViewLL = myInterface.getMyInterfaceMethod();
}
Note that this check is not required :
if (myClass instanceof IMyInterface) {
View revPluggableViewLL = myClass.getMyInterfaceMethod();
}
You manipulate a List
of IMyInterface
, so elements of the List
are necessarily instances of IMyInterface
.
myClass
is an instance of Class
, which doesn't implement IMyInterface
(even if it's Class<IMyInterface>
). Therefore you can never execute getMyInterfaceMethod()
on myClass
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.