i have this class
class GameLoaderClass<V> {
V classValue = null;
Class<?> aClass;
ClassType classType;
public GameLoaderClass(ClassType classType, Class<?> aClass) {
this.classType = classType;
this.aClass = aClass;
}
public V getClassType(String name) {
if (this.classType == ClassType.FIELD) {
return this.classValue = aClass.getDeclaredField(name);
} else
return this.classValue = aClass.getMethod(name);
}
But i get this following error..
Required type: C Provided: Method
any way to fix it?, thanks
I would be better to use return type Object
instead of using generics.
public Member getClassType(String name) {
if (this.classType == ClassType.FIELD) {
return aClass.getDeclaredField(name);
}
return aClass.getMethod(name);
}
And in the place where you call the method you could do
Member obj = loader.getClassType("someName");
if(o instance of Field) {
Field f = (Field) obj;
// do the rest
} else /* Assuming Field or Method are the possible types of returned objects */ {
Method m = (Method) obj;
// do the rest
}
EDIT: Based on @Onkar's comment, it would be better to return an object of type Member
instead of Object
as both Method
and Field
implement Member
.
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.