Get child class name from parent

前端 未结 6 595
广开言路
广开言路 2021-02-06 21:36

I have a base class for my all of activities (ActivityBase) that itself derives from android.app.Activity. In onCreate I want to execute some conditio

6条回答
  •  你的背包
    2021-02-06 22:02

    Use instanceof operator.

    Supposing you have a base class and two subclasses named Base, SubOne and SubTwo, if you want to check if a variable ref is an instance of SubOne or SubTwo you'd say:

    if(ref instanceof SubOne){
    }
    else if(ref instanceof SubTwo){
    }
    

    Note that: (ref instanceof Base) will always return true though.

提交回复
热议问题