是否可以将类内类的实例设置为 null。例如,我可以做这样的事情吗
int main{
//Create a new test object
Test test = new Test();
//Delete that object. This method should set the object "test" to null,
//thus allowing it to be called by the garbage collector.
test.delete();
}
public class Test{
public delete(){
this = null;
}
}
我已经尝试过这个,但它不起作用。使用“this = null”我得到左侧需要是变量的错误。有没有办法实现类似的目标?
对象的实例不知道哪些引用可能正在引用它,因此对象中的代码无法将这些引用设为空。你所要求的是不可能的(*)。
* at least not without adding a pile of scaffolding to keep track of all the references, and somehow inform their owners that they should be nulled - in no way would it be "Just for convenience".
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)