avoid instanceof in Java

前端 未结 9 2267
清歌不尽
清歌不尽 2021-02-12 20:40

I have been told at some stage at university (and have subsequently read in upteen places) that using instanceof should only be used as a \'last resort\'. With this

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-12 21:05

    You could use the mGameObjects container for when you need to do something on all game objects and keep a separate container only for GameGroup objects.

    This will use some more memory, and when you add/remove objects you have to update both containers, but it shouldn't be a noticeable overhead, and it lets you loop very efficiently through all the objects.

提交回复
热议问题