标题:[求助]一个缠了我好久的问题~
只看楼主
snakealpha
Rank: 1
来 自:扬州
等 级:新手上路
威 望:1
帖 子:267
专家分:0
注 册:2005-11-5
 问题点数:0 回复次数:0 
[求助]一个缠了我好久的问题~
发在博客上没人理,所以转发在这里~~~

以下代码:

Imports Microsoft.VisualBasic
Imports system


Public Class hello
Private myName As String
Public Sub New(ByVal name As String)
myname = name
console.writeline("Hello,{0}!", name)
End Sub
Protected Overrides Sub finalize()
console.writeline("GoodBye,{0}!", myname)

End Sub

End Class
Public Class application
Public Shared Sub main()
Dim i As Integer
For i = 1 To 5
Dim hello As New hello(i.tostring)
Next i

End Sub
End Class

得到结果:

Hello,1!
Hello,2!
Hello,3!
Hello,4!
Hello,5!
GoodBye,5!
GoodBye,1!
GoodBye,4!
GoodBye,3!
GoodBye,2!

书上解释为:

"看起来对象似乎是按照与创建对象相反的顺序释放的,表明是插入到了一个堆栈是的结构中.事实上这就是所发生的事情."(VisualBasic.Net面向对象编程,J.P.Hamilton)

但是以下例子:

Imports Microsoft.VisualBasic
Imports system


Public Class hello
Private bigchunkomemory(10000) As Byte
Private myName As String
Public Sub New(ByVal name As String)
myname = name
console.writeline("Hello,{0}!", name)
End Sub
Protected Overrides Sub finalize()
console.writeline("GoodBye,{0}!", myname)

End Sub

End Class
Public Class application
Public Shared Sub main()
Dim i As Integer
For i = 1 To 10000
Dim hello As New hello(i.tostring)

Next i

End Sub
End Class

输出的结果中有一段:

Hello,112!
Hello,113!
Hello,114!
Hello,115!
GoodBye,101!
GoodBye,15!
GoodBye,14!
GoodBye,13!

另一段:

GoodBye,3!
GoodBye,2!
GoodBye,1!
GoodBye,100!
GoodBye,99!
GoodBye,98!
GoodBye,97!

怎么会15~1会比100先释放呢?如果如书上说是“堆栈”的话?

2007-08-26 16:13



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-165543-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.095833 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved