如何在Dart中删除对象? [英] How do we delete an object in Dart?

查看:603
本文介绍了如何在Dart中删除对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基本上删除我创建的对象。我们如何删除对象?

I want to basically delete an object I created. How do we delete an object?

我检查了对象定义这里,但无法找出办法做到这一点。另外,我很好奇是否可以定义析构函数。

I checked Object definition here, but couldn't figure out way to do it. Also I am curious whether we can define destructors or not.

UPDATE
问题是得到好的答案。但我想提请你注意一个案例,我想删除我的对象或调用析构函数。假设我们想创建一个速度,你可以通过放置在它上面的端口连接矩形。所以这个想法是有一个对象,它有一个引用矩形的主体和端口放置在两端。实际上,该对象可能需要一些其他属性,例如 [bool] selected [bool] dragging [List< RectElement>] connectedSquares 。例如,当用户选择矩形并击退退格时,我想确保矩形已经删除,我的对象被正确删除。

UPDATE The question is getting good answers. But I want to draw your attention to a case in which I want to delete my objects or call destructor. Let's say we want to create a pace using that you can connect rectangles via the ports placed on it. So the idea is to have an object that has a reference to the body of the rectangle and the ports placed at two ends. In fact, that object might need some other properties like [bool] selected or [bool] dragging or [List<RectElement>] connectedSquares. For example, when user selects the rectangle and hits backspace, I want to make sure the rectangles are gone and my object is properly deleted. So this use case may give some more insight into the question.

推荐答案

您不需要主动删除Dart中的对象。

You don't need to actively delete objects in Dart.

Dart是一种垃圾回收语言,因此任何没有引用的对象最终都会被运行时系统垃圾回收和释放。

Dart is a garbage-collected language, so any object that you don't hold any references to will eventually be garbage collected and freed by the runtime system.

所以你要做的就是清除引用对象的任何变量。

So all you have to do is to clear any variables you have that reference the object.

垃圾回收在语言规范中。规范没有说,所以垃圾回收存在(它甚至没有提到的概念),但语言和库设计的方式,垃圾回收是可能的:程序无法找到一个引用的任何地方的对象不能影响程序的行为,所以它是不可检测的,他们被垃圾收集器收集和删除。

Garbage collection is really implicit in the language specification. The specification doesn't say so that garbage collection exists (it doesn't even mention the concept), but the language and libraries are designed in such a way that garbage collection is possible: Objects that the program cannot find a reference to anywhere also can't affect the program's behavior any more, so it is undetectable that they are being collected and deleted by the garbage collector.

这篇关于如何在Dart中删除对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆