instanceof keyword usage

后端 未结 8 2277
野性不改
野性不改 2021-02-14 10:44

Is using the instanceof keyword against the essence of object oriented programming? I mean is it a bad programming practice? I read somewhere that us

8条回答
  •  悲&欢浪女
    2021-02-14 11:12

    Usage of instanceof is discouraged when same effect can be achieved via virtual methods, like in example of thomson_matt. However, it's necessary to use instanceof in some circumstances. For example, when your code gets Object from external source, say, network or third-party API which returns Object, and you must decide what is the type of this Object and act appropriately.

提交回复
热议问题