When should and shouldn't instanceof be used?

后端 未结 5 635
情书的邮戳
情书的邮戳 2020-12-09 10:50

I have gone through various articles, but I still do not know why instanceof should not be used. Kindly let me know your thoughts.

5条回答
  •  伪装坚强ぢ
    2020-12-09 11:08

    Casting from a base type to a derived type is a bad thing. If you use instandof that way, it's considered bad design because hard to maintain and read. See http://www.javapractices.com/topic/TopicAction.do?Id=31.

    Using instanceof for equals() because you have an object that should be your type, that's good practice.

提交回复
热议问题