Can an object remove itself? How?

前端 未结 5 864
我在风中等你
我在风中等你 2021-01-17 15:31

I\'m trying to write a simple ball game, and there\'s several turns (ie., ball lives). The ball \"dies\" when it passes the bottom border of the screen. What I have so far w

5条回答
  •  迷失自我
    2021-01-17 16:06

    In a sense of deleting the object from memory: no, in Java that is handled by the garbage collector exclusively.

    What you could do is to remove the object from collections containing it, but this would require the object to have access to those collections (which in most cases would not be feasible, since there might be a lot of collections).

    I'd suggest the containing object (the screen in your case) to poll for the contained object's (the ball's) state and remove it after it is actually dead.

提交回复
热议问题