哈哈,我这样实现了:
dim collection_temp as new collection
dim class_temp as class1set class_temp as new class1
class_temp.aa="aaa"
collection_temp.add class_tempset class_temp as new class1
class_temp.aa="bbb"
collection_temp.add class_temp不过我不理解 set new 实例的生存期
dim collection_temp as new collection
dim class_temp as class1set class_temp as new class1
class_temp.aa="aaa"
collection_temp.add class_tempset class_temp as new class1
class_temp.aa="bbb"
collection_temp.add class_temp不过我不理解 set new 实例的生存期
dim a1 as new class1
dim a2 as new class2
dim class_temp as class1
set class_temp=new class1
collection_temp.add class_temp
'其他代码
set class_temp=new class1
collection_temp.add class_temp
当所有指向它的引用都设成nothing
则该对象就会被释放
開始
當你結束對象時, set new 实例的生存期
就結束
可能不会
所以保险一点当用完以后都要set =nothing
否则内存会溢出
到底怎么回事,我要写一个类似服务的程序,可不能跑没两天就拖跨机器了!!!!!
dim collection_temp as new collection
dim class_temp as class1
set class_temp as new class1
class_temp.aa="aaa"
collection_temp.add class_temp
set class_temp=nothingset class_temp as new class1
class_temp.aa="bbb"
collection_temp.add class_temp
set class_temp=nothing仅供参考
new是会自动帮你释放原来的引用
但是最后一个set=nothing不能少关于nothing,msdn有以下论述,可作参考
Set MyObject = Nothing
Several object variables can refer to the same actual object. When Nothing is assigned to an object variable, that variable no longer refers to any actual object. When several object variables refer to the same object, memory and system resources associated with the object to which the variables refer are released only after all of them have been set to Nothing, either explicitly using Set, or implicitly after the last object variable set to Nothing goes out of scope.
dim class_temp as class1
set class_temp as new class1
class_temp.aa="aaa"
collection_temp.add class_temp
'set class_temp=nothing可以没有set class_temp as new class1
class_temp.aa="bbb"
collection_temp.add class_temp
set class_temp=nothing'一定要
在wrap一个index属性,这样可以根据动态生成的类的index去结束相应的类
便于管理
另外,你们的collection为什么不把自定义的class包进去呢---即让你的类成为collection
的item,否则,在这里用collection对象有什么意义
一种是引用计数,就是楼上所说的那个意思,有个计数属性 set的时候+1,set =nothing时-1,=0释放自己
还有一种叫做垃圾回收,就是到资源使用超出一定的范围,系统来一次scan看看那些对象已经没有引用指向它了,然后释放
但这些实现不用你考虑,只需要了解就行了
dim class_temp as class1
set class_temp as new class1
class_temp.aa="aaa"
collection_temp.add class_temp
set class_temp=nothingset class_temp as new class1
class_temp.aa="bbb"
collection_temp.add class_temp
set class_temp=nothing
这样加一个 nothing一个不行,nothing后不能访问原指向的项目。
”new是会自动帮你释放原来的引用,但是最后一个set=nothing不能少“,这样看来也讲不通。
一下是摘自msdn
Set objectvar = {objectexpression | New classname | Nothing}NewKeyword used to create a new instance of a class. If objectvar contained a reference to an object, that reference is released when the new one is assigned. The New keyword can only be used to create an instance of a class.
相当于自动在前面加一句set =nothing
最后要加是因为如果声明class_temp所在的函数结束了,则该变量就会被释放,而引用并没有被释放
的时候其实对象并没有释放,因为在collection里面还有一个引用是指向它的
你说的对,我刚才看错了,第一个class_temp,set nothing 后,collection_temp还可引用。谢谢‘葡萄’.
vb 版比其他版,好多了。有这么多热情的兄弟帮忙。希望各位,团结一心,继续努力,共同提高!!!!!!!!