I would like to get param2
value from a extended class that has common properties shared with an abstract class 'B' like this:
import java.lang.reflect.Field;
public class Fields {
public class A{
public abstract class B{
int param1;
String param2;
public abstract void B ();
}
public class B0 extends B{
@Override
public void B() {
this.param1 = 0;
this.param2 = "Mario";
}
}
public class B1 extends B{
@Override
public void B() {
this.param1 = 1;
this.param2 = "Anna";
}
}
B1 b1 = new B1();
public A (){
b1.B();
}
public B0 GetB(Object AnyB){
B0 tmp_b0 = (B0) AnyB;
return tmp_b0;
}
}
public Fields(){
A a = new A();
Field[] f = a.getClass().getDeclaredFields();
for (int i = 0; i<f.length; i++){
f[i].setAccessible(true);
Object obj = f[i];
String p2;
p2 = a.GetB(obj).param2;
System.out.println(p2);
}
}
public static void main(String [] args){
Fields f = new Fields();
}
}
Formally the instruction B0 tmp_b0 = (B0) AnyB;
is correct, but in runtime I get the error : Exception in thread "main" java.lang.ClassCastException: java.lang.reflect.Field cannot be cast to Fields$A$B0
.
Why ?
There is a way to get the value of parameter param1
or param2
.
There is a way ?
Thanks
Aucun commentaire:
Enregistrer un commentaire