lundi 3 février 2020

How to get filed type by reflection at run time?

How to get class type(A1 or A2) of base in test method by reflection at run time ? I tried this.getClass().getField("base").getType(); but getType returns declared type, Is there any method can do this?

Thanks in advance!

public class Base{

}

public class A1 extends Base{

}

public class A2 extends Base{

}

public abstract class Cmd{
    public Base base;
    public void test(){
        this.getClass().getField("base").getType();
    }

}

public class Cmd1 extends Cmd{
    public Cmd1(Base base){
        this.base = base;

    }
}


public class Cmd2 extends Cmd{
    public Cmd2(Base base){
        this.base = base;
    }
}

Base testBase = new A1();
Cmd myCmd = new Cmd1(testBase);




Aucun commentaire:

Enregistrer un commentaire