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