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

后端 未结 6 1826
我寻月下人不归
我寻月下人不归 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:19

    The answer to your question is instanceof.

    However, keep in mind that if your code needs instanceof, it's a sign that something is not right with your design. There are some cases when instanceof is justified, but they are rather exceptions. Usually, if your subclasses need to behave differently, you have to use polymorphism instead of if()s.

提交回复
热议问题