Does Java have an “is kind of class” test method

后端 未结 6 1827
我寻月下人不归
我寻月下人不归 2021-02-03 22:01

I have a baseclass, Statement, which several other classes inherit from, named IfStatement, WhereStatement, etc... What is the best way t

6条回答
  •  借酒劲吻你
    2021-02-03 22:25

    if(object instanceof WhereStatement) {
       WhereStatement where = (WhereStatement) object;
       doSomething(where);
    }
    

    Note that code like this usually means that your base class is missing a polymorphic method. i.e. doSomething() should be a method of Statement, possibly abstract, that is overridden by sub-classes.

提交回复
热议问题