使用构造函数和析构函数
构造函数和析构函数控制对象的创建和毁坏。
若要为类创建构造函数,请在类定义的任何位置创建名为 Sub New 的过程。若要创建参数化构造函数,请像为其他任何过程指定参数那样为 Sub New 指定参数的名称和数据类型,如下面的代码所示:
Sub New()Sub New(ByVal sString As String)
``
构造函数频繁地重载,如下面的代码所示:
``
![ExpandedBlockStart.gif][] ![ContractedBlock.gif][ExpandedBlockStart.gif] Sub New() SubNew(ByVal sString AsString, iInt AsInteger)
当定义从另一个类派生的类时,构造函数的第一行必须是对基类构造函数的调用,除非基类有一个可访问的无参数构造函数。例如,对包含以上构造函数的基类的调用将为 MyBase.New(sString)
。另外,MyBase.New
是可选的,Visual Basic .NET 运行库会隐式调用它。
在编写调用父对象的构造函数的代码后,可以向 Sub New 过程添加任何附加初始化代码。当 Sub New 作为参数化构造函数调用时可以接受参数。这些参数从调用构造函数的过程传递(例如 Dim AnObject As New ThisClass(X)
)。
下面的代码实例展示如何使用 Dispose 和 Finalize 释放资源:
![None.gif][ExpandedBlockStart.gif] ‘ Design pattern for a base class.
![ExpandedBlockStart.gif][]![ContractedBlock.gif][ExpandedBlockStart.gif] Public Class Base Class Base
![InBlock.gif][ExpandedBlockStart.gif]Implements IDisposable
![InBlock.gif][ExpandedBlockStart.gif]’ Implement IDisposable.
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]PublicOverloadsSub Dispose()Sub Dispose() Implements IDisposable.Dispose
![InBlock.gif][ExpandedBlockStart.gif] Dispose(True)
![InBlock.gif][ExpandedBlockStart.gif] GC.SuppressFinalize(Me)
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![InBlock.gif][ExpandedBlockStart.gif]
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]ProtectedOverridableOverloadsSub Dispose()Sub Dispose(ByVal disposing AsBoolean)
![InBlock.gif][ExpandedBlockStart.gif]If disposing Then
![InBlock.gif][ExpandedBlockStart.gif]’ Free other state (managed objects).
![InBlock.gif][ExpandedBlockStart.gif]EndIf
![InBlock.gif][ExpandedBlockStart.gif]’ Free your own state (unmanaged objects).
![InBlock.gif][ExpandedBlockStart.gif]’ Set large fields to null.
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![InBlock.gif][ExpandedBlockStart.gif]
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]ProtectedOverridesSub Finalize()Sub Finalize()
![InBlock.gif][ExpandedBlockStart.gif]’ Simply call Dispose(False).
![InBlock.gif][ExpandedBlockStart.gif] Dispose(False)
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![ExpandedBlockEnd.gif][ExpandedBlockStart.gif]End Class
![None.gif][ExpandedBlockStart.gif]
![None.gif][ExpandedBlockStart.gif] ‘ Design pattern for a derived class.
![ExpandedBlockStart.gif][]![ContractedBlock.gif][ExpandedBlockStart.gif] Public Class Derived Class Derived
![InBlock.gif][ExpandedBlockStart.gif]Inherits Base
![InBlock.gif][ExpandedBlockStart.gif]
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]ProtectedOverloadsOverridesSub Dispose()Sub Dispose(ByVal disposing AsBoolean)
![InBlock.gif][ExpandedBlockStart.gif]If disposing Then
![InBlock.gif][ExpandedBlockStart.gif]’ Release managed resources.
![InBlock.gif][ExpandedBlockStart.gif]EndIf
![InBlock.gif][ExpandedBlockStart.gif]’ Release unmanaged resources.
![InBlock.gif][ExpandedBlockStart.gif]’ Set large fields to null.
![InBlock.gif][ExpandedBlockStart.gif]’ Call Dispose on your base class.
![InBlock.gif][ExpandedBlockStart.gif]MyBase.Dispose(disposing)
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![InBlock.gif][ExpandedBlockStart.gif]’ The derived class does not have a Finalize method
![InBlock.gif][ExpandedBlockStart.gif]’ or a Dispose method with parameters because it inherits
![InBlock.gif][ExpandedBlockStart.gif]’ them from the base class.
![ExpandedBlockEnd.gif][ExpandedBlockStart.gif]End Class
下面的示例说明使用 Dispose 析构函数的一个通用设计模式:
![None.gif][ExpandedBlockStart.gif] Dim con As Connection, rs As RecordSet
![None.gif][ExpandedBlockStart.gif] Try
![None.gif][ExpandedBlockStart.gif] con = New Connection( “ xyz “ )
![None.gif][ExpandedBlockStart.gif] rs = New RecordSet(con, “ MyTable “ )
![None.gif][ExpandedBlockStart.gif] ‘ Use the connection if successful.
![None.gif][ExpandedBlockStart.gif] Finally
![None.gif][ExpandedBlockStart.gif] ‘ Call the Dispose method when done with any created objects.
![None.gif][ExpandedBlockStart.gif] If Not con Is Nothing Then
![None.gif][ExpandedBlockStart.gif] con.Dispose()
![None.gif][ExpandedBlockStart.gif] End If
![None.gif][ExpandedBlockStart.gif] If Not rs Is Nothing Then
![None.gif][ExpandedBlockStart.gif] rs.Dispose()
![None.gif][ExpandedBlockStart.gif] End If
![None.gif][ExpandedBlockStart.gif] End Try
下一个示例使用参数化构造函数创建一个对象,并在不再需要该对象时调用析构函数:
![ExpandedBlockStart.gif][] ![ContractedBlock.gif][ExpandedBlockStart.gif] Sub TestConstructorsAndDestructors() Sub TestConstructorsAndDestructors()
![InBlock.gif][ExpandedBlockStart.gif]Dim X AsInteger=6
![InBlock.gif][ExpandedBlockStart.gif]Dim AnObject AsNew ThisClass(X)
![InBlock.gif][ExpandedBlockStart.gif]’ Place statements here that use the object.
![InBlock.gif][ExpandedBlockStart.gif] AnObject.DoSomething()
![InBlock.gif][ExpandedBlockStart.gif]’ Test the parameterized constructor.
![InBlock.gif][ExpandedBlockStart.gif]MsgBox(“The value of ThisProperty after being initialized “& _
![InBlock.gif][ExpandedBlockStart.gif]” by the constructor is “& AnObject.ThisProperty)
![InBlock.gif][ExpandedBlockStart.gif]’ Run the Dispose method when you are done with the object.
![InBlock.gif][ExpandedBlockStart.gif] AnObject.Dispose()
![ExpandedBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![None.gif][ExpandedBlockStart.gif]
![ExpandedBlockStart.gif][]![ContractedBlock.gif][ExpandedBlockStart.gif] Public Class BaseClass Class BaseClass
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]Sub New()SubNew()
![InBlock.gif][ExpandedBlockStart.gif]MsgBox(“The base class is initializing with Sub New.”)
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![InBlock.gif][ExpandedBlockStart.gif]
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]ProtectedOverridesSub Finalize()Sub Finalize()
![InBlock.gif][ExpandedBlockStart.gif]MsgBox(“The base class is destroying objects with Sub Finalize.”)
![InBlock.gif][ExpandedBlockStart.gif]’ Place final cleanup tasks here.
![InBlock.gif][ExpandedBlockStart.gif]MyBase.Finalize()
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![ExpandedBlockEnd.gif][ExpandedBlockStart.gif]End Class
![None.gif][ExpandedBlockStart.gif]
![ExpandedBlockStart.gif][]![ContractedBlock.gif][ExpandedBlockStart.gif] Public Class ThisClass Class ThisClass
![InBlock.gif][ExpandedBlockStart.gif]Inherits BaseClass
![InBlock.gif][ExpandedBlockStart.gif]Implements IDisposable ‘ Implements the Dispose method of IDisposable.
![InBlock.gif][ExpandedBlockStart.gif]Private m_PropertyValue AsInteger
![InBlock.gif][ExpandedBlockStart.gif]
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]Sub New()SubNew(ByVal SomeValue AsInteger)
![InBlock.gif][ExpandedBlockStart.gif]MyBase.New() ‘ Call MyBase.New if this is a derived class.
![InBlock.gif][ExpandedBlockStart.gif]MsgBox(“Sub New is initializing the class ThisClass.”)
![InBlock.gif][ExpandedBlockStart.gif]’ Place initialization statements here.
![InBlock.gif][ExpandedBlockStart.gif] m_PropertyValue = SomeValue
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![InBlock.gif][ExpandedBlockStart.gif]
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]Property ThisProperty()Property ThisProperty() AsInteger
![InBlock.gif][ExpandedBlockStart.gif]Get
![InBlock.gif][ExpandedBlockStart.gif] ThisProperty = m_PropertyValue
![InBlock.gif][ExpandedBlockStart.gif]EndGet
![InBlock.gif][ExpandedBlockStart.gif]Set(ByVal Value AsInteger)
![InBlock.gif][ExpandedBlockStart.gif] m_PropertyValue = Value
![InBlock.gif][ExpandedBlockStart.gif]EndSet
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Property
![InBlock.gif][ExpandedBlockStart.gif]
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]Sub DoSomething()Sub DoSomething()
![InBlock.gif][ExpandedBlockStart.gif]’ Place code here that does some task.
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![InBlock.gif][ExpandedBlockStart.gif]
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]ProtectedOverridesSub Finalize()Sub Finalize()
![InBlock.gif][ExpandedBlockStart.gif]MsgBox(“Finalize is destroying objects in ThisClass.”)
![InBlock.gif][ExpandedBlockStart.gif]’ Place final cleanup tasks here.
![InBlock.gif][ExpandedBlockStart.gif]MyBase.Finalize()
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![InBlock.gif][ExpandedBlockStart.gif]
![ExpandedSubBlockStart.gif][ExpandedBlockStart.gif]![ContractedSubBlock.gif][ExpandedBlockStart.gif]OverridableSub Dispose()Sub Dispose() Implements IDisposable.Dispose
![InBlock.gif][ExpandedBlockStart.gif]MsgBox(“The ThisClass is running Dispose.”)
![ExpandedSubBlockEnd.gif][ExpandedBlockStart.gif]End Sub
![ExpandedBlockEnd.gif][ExpandedBlockStart.gif]End Class
当运行此示例时,ThisClass
类调用 BaseClass
类的 Sub New 构造函数。在基类中的构造函数完成以后,ThisClass
类运行 Sub New 中剩余的语句,这些语句初始化 ThisProperty
属性的值。
当不再需要该类时,在 ThisClass
中调用 Dispose 析构函数。
如果最初是从窗体创建 ThisClass
的实例,则在关闭该窗体之前似乎什么都没有发生。Finalize 析构函数此时在 ThisClass
类中运行,最后将在 BaseClass
类中运行。
转载于//www.cnblogs.com/zman/archive/2006/07/25/458903.html
[ExpandedBlockStart.gif]:
还没有评论,来说两句吧...