java 物体可以移除自身吗?如何?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7270497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:21:50  来源:igfitidea点击:

Can an object remove itself? How?

javaobject

提问by Alicja Z

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 works, but doesn't seem to be the proper way to do things:

我正在尝试编写一个简单的球类游戏,并且有几个回合(即球命)。当球通过屏幕的底部边界时,球“死亡”。到目前为止我所做的工作,但似乎不是做事的正确方法:

if (ball.getY() > bottomOfScreen) {
  ball.die();
  remove(ball);
}

The die() method basically fades the ball's colour slowly (dark_gray -> pause(50) -> light_gray -> pause(50)), but doesn't actually do anything useful.

die() 方法基本上会缓慢地使球的颜色褪色(dark_gray -> pause(50) -> light_gray -> pause(50)),但实际上并没有做任何有用的事情。

The remove(), obviously, gets rid of the ball from the screen, which is what I want. It makes sense to me for this remove() to be a part of Ball's die() method, as opposed to it being a separate method call in the main program -- but I'm not sure how to go about this?

显然,remove() 从屏幕上去除了球,这正是我想要的。对我来说,这个 remove() 成为 Ball 的 die() 方法的一部分是有意义的,而不是它是主程序中的一个单独的方法调用——但我不确定如何去做?

Can an object delete itself? And, if it can, is object suicide better than object murder, from a philosophical/methodological point of view?

对象可以删除自己吗?而且,如果可以的话,从哲学/方法论的角度来看,对象自杀是否比对象谋杀更好?

Thanks!

谢谢!

采纳答案by Johan Sj?berg

The object can remove itself given it has some sort of reference to the view rendering mechanism. Your sample doesn't give enough information so I'll exemplify one way to do it:

对象可以移除自身,因为它对视图渲染机制有某种引用。您的示例没有提供足够的信息,因此我将举例说明一种方法:

public class Ball {
    private ViewRenderer view;

    public void remove() {
       view.remove(this);
    }
}

Neither suicidenor murderis better or worse. It depends on your design and requirements.

无论是自杀还是谋杀都没有好坏之分。这取决于您的设计和要求。

In this sample though, murdermight be preferablesince this way the Ballobject doesn'tneed to know in which context it's being used.

但是在这个示例中,谋杀可能更可取,因为这样Ball对象不需要知道它在哪个上下文中被使用。

回答by DJClayworth

It is possible to create a method in which the ball removes itself, but it's a bad thing to do. In order to remove itself from the screen, the Ball must have a reference to the screen. This creates a circular chain of references (Ball has a reference to screen, screen has a reference to Ball) which is going to make your design more complicated and your testing much more complicated.

可以创建一种让球自行移开的方法,但这是一件坏事。为了从屏幕上移除自己,球必须有一个对屏幕的引用。这会创建一个循环引用链(Ball 引用 screen,screen 引用 Ball),这将使您的设计更加复杂,并且您的测试更加复杂。

Suicide is fine - the screen tells the ball to die, and the ball dies. But this is about removal of a relationship, not dying. The thing maintaining the relationship is the screen, and so it should be the thing doing the removal.

自杀没问题——屏幕告诉球死了,球就死了。但这是关于解除关系,而不是死亡。维持关系的东西是屏幕,所以它应该是做删除的东西。

Also remember that the two do not necessarily have to happen together. The screen might want to keep a dead ball around for some reason, and it might want to remove a ball that isn't dead. Even if that doesn't happen in your app right now, allow for the possibility.

另请记住,这两者不一定必须同时发生。屏幕可能出于某种原因想要保留一个死球,并且可能想要移除一个没有死的球。即使现在您的应用程序中没有发生这种情况,也要考虑到这种可能性。

回答by Thomas

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

在从内存中删除对象的意义上:不,在 Java 中由垃圾收集器专门处理。

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.

我建议包含对象(在您的情况下为屏幕)轮询包含对象(球的)状态并在它实际死亡后将其删除。

回答by Ben

There is presumably some object (e.g. the Screen, or the ViewRenderer in Johan's example) that holds a reference to the Ball, and removing this reference has to be done by the Screen ("object murder"). "Object suicide" amounts to Ball passing a message to the Screen asking to be "murdered".

大概有一些对象(例如 Screen,或 Johan 示例中的 ViewRenderer)持有对 Ball 的引用,并且删除该引用必须由 Screen 完成(“对象谋杀”)。“对象自杀”相当于 Ball 向屏幕传递一条信息,要求“谋杀”。

Since it is the Ball that knows when it has passed the boundary, it makes sense to me (without knowing the details of your design) for the removal to be initiated by the Ball. Then the Screen can find out about this change by one of several means:

由于是 Ball 知道它何时通过了边界,因此由 Ball 发起移除对我来说是有意义的(不知道您的设计细节)。然后,屏幕可以通过以下几种方式之一了解此更改:

  • The Screen can poll the Ball.
  • The Ball can hold a direct backward reference to the Screen, which creates an unfortunate circular dependency.
  • The Ball can hold a reference to the screen via a BallObserverinterface. This is an application of the observer pattern.
  • 屏幕可以轮询球。
  • Ball 可以保存对 Screen 的直接向后引用,这会造成不幸的循环依赖。
  • Ball 可以通过BallObserver接口保存对屏幕的引用。这是观察者模式的一个应用。

The first is simplest, and this makes it a good choice if it fits naturally into your mechanism for painting the screen. The third is more flexible in principle, but you might not need this flexibility in practice. The middle option might be OK in a simple program, but you should probably consider it as a step on the way to the third.

第一个是最简单的,如果它自然适合您的屏幕绘制机制,这将是一个不错的选择。第三种原则上更灵活,但您在实践中可能不需要这种灵活性。在一个简单的程序中,中间选项可能没问题,但您可能应该将其视为通往第三个选项的一步。

And if you don'thave a Screen (or ViewRenderer, or whatever) object and really mean "a separate method call in the main program" then you should probably reconsider your design.

如果您没有Screen(或 ViewRenderer 或其他)对象并且真正的意思是“主程序中的单独方法调用”,那么您可能应该重新考虑您的设计。

回答by Joseph Hansen

No, objects cannot suicide. Any reference of itself is just a reference.

不,物体不能自杀。任何对自身的引用都只是一个引用。

To "clear" the object within itself one would just clear all instance variables.

要“清除”自身内部的对象,只需清除所有实例变量。

To "clear" the object outside itself one would set the variable equal to null.

要“清除”自身之外的对象,可以将变量设置为 null。