对象方法的实现 [英] object method implementation
本文介绍了对象方法的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在一篇文章中读到,对象只是在程序执行期间存在.
I read in an article that objects there is just during program implementation. Is it correct?
推荐答案
是.
当您的程序中创建的对象超出范围"时,它们将被销毁:控制流从声明对象的块退出,即为"if"块:
Yes.
Objects you create in your program are destroyed when they "go out of scope": I.e. the flow of control exits from the block in which the object was declared, be that an "if" block:
if(myCondition)
{
int myInt;
...
} // myInt is out of scope here, and can be destroyed.
或方法:
private void myMethod()
{
int myInt;
...
} // myInt is out of scope here, and can be destroyed.
程序结束时,所有对象都超出范围,可以被销毁.
[edit]
重新读一遍,就意味着所有对象都被销毁了,这是不正确的:只要有对对象的引用,就不符合销毁的条件,因此您可以将引用传递给对象的范围之外.它是创建的.
When your program ends, all objects are out of scope, and can be destroyed.
[edit]
Reading back on that, it implies that all objects are destroyed, which is not true: as long as there is a reference to an object, it is not eligible for destruction, so you can pass a reference to an object outside the scope in which it was created.
Point myPoint;
myMethod();
...
private void myMethod()
{
int myInt;
Point p = new Point(100,100);
myPoint = p;
} // myInt is out of scope here and so is p, but myPoint is
// referring to the Point object, so it will not be destroyed.
[/edit]
这篇关于对象方法的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文