使用构造函数和析构函数

构造函数和析构函数控制对象的创建和毁坏。

若要为类创建构造函数,请在类定义的任何位置创建名为 Sub New 的过程。若要创建参数化构造函数,请像为其他任何过程指定参数那样为 Sub New 指定参数的名称和数据类型,如下面的代码所示:


ExpandedBlockStart.gifContractedBlock.gifSub New()Sub New(ByVal sString As String)

构造函数频繁地重载,如下面的代码所示:

 

ExpandedBlockStart.gifContractedBlock.gifSub New()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))。

下面的代码实例展示如何使用 DisposeFinalize 释放资源:

None.gif' Design pattern for a base class.
ExpandedBlockStart.gifContractedBlock.gif
Public Class BaseClass Base
InBlock.gif    
Implements IDisposable
InBlock.gif    
' Implement IDisposable.
ExpandedSubBlockStart.gifContractedSubBlock.gif
    Public Overloads Sub Dispose()Sub Dispose() Implements IDisposable.Dispose
InBlock.gif        Dispose(
True)
InBlock.gif        GC.SuppressFinalize(
Me)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected Overridable Overloads Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
InBlock.gif        
If disposing Then
InBlock.gif            
' Free other state (managed objects).
InBlock.gif
        End If
InBlock.gif        
' Free your own state (unmanaged objects).
InBlock.gif
        ' Set large fields to null.
ExpandedSubBlockEnd.gif
    End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected Overrides Sub Finalize()Sub Finalize()
InBlock.gif        
' Simply call Dispose(False).
InBlock.gif
        Dispose(False)
ExpandedSubBlockEnd.gif    
End Sub

ExpandedBlockEnd.gif
End Class

None.gif
None.gif
' Design pattern for a derived class.
ExpandedBlockStart.gifContractedBlock.gif
Public Class DerivedClass Derived
InBlock.gif    
Inherits Base
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
Protected Overloads Overrides Sub Dispose()Sub Dispose(ByVal disposing As Boolean)
InBlock.gif        
If disposing Then
InBlock.gif            
' Release managed resources.
InBlock.gif
        End If
InBlock.gif        
' Release unmanaged resources.
InBlock.gif
        ' Set large fields to null.
InBlock.gif
        ' Call Dispose on your base class.
InBlock.gif
        MyBase.Dispose(disposing)
ExpandedSubBlockEnd.gif    
End Sub

InBlock.gif    
' The derived class does not have a Finalize method
InBlock.gif
    ' or a Dispose method with parameters because it inherits
InBlock.gif
    ' them from the base class.
ExpandedBlockEnd.gif
End Class

下面的示例说明使用 Dispose 析构函数的一个通用设计模式:

None.gifDim con As Connection, rs As RecordSet
None.gif
Try
None.gif   con 
= New Connection("xyz")
None.gif   rs 
= New RecordSet(con, "MyTable")
None.gif   
' Use the connection if successful.
None.gif
Finally
None.gif
' Call the Dispose method when done with any created objects.
None.gif
If Not con Is Nothing Then
None.gif      con.Dispose()
None.gif   
End If
None.gif   
If Not rs Is Nothing Then
None.gif      rs.Dispose()
None.gif   
End If
None.gif
End Try

下一个示例使用参数化构造函数创建一个对象,并在不再需要该对象时调用析构函数:

ExpandedBlockStart.gifContractedBlock.gifSub TestConstructorsAndDestructors()Sub TestConstructorsAndDestructors()
InBlock.gif   
Dim X As Integer = 6
InBlock.gif   
Dim AnObject As New ThisClass(X)
InBlock.gif   
' Place statements here that use the object.
InBlock.gif
   AnObject.DoSomething()
InBlock.gif   
' Test the parameterized constructor. 
InBlock.gif
   MsgBox("The value of ThisProperty after being initialized " & _
InBlock.gif          
" by the constructor is " & AnObject.ThisProperty)
InBlock.gif   
' Run the Dispose method when you are done with the object.
InBlock.gif
   AnObject.Dispose()
ExpandedBlockEnd.gif
End Sub

None.gif
ExpandedBlockStart.gifContractedBlock.gif   
Public Class BaseClassClass BaseClass
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Sub New()Sub New()
InBlock.gif         
MsgBox("The base class is initializing with Sub New.")
ExpandedSubBlockEnd.gif      
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Protected Overrides Sub Finalize()Sub Finalize()
InBlock.gif         
MsgBox("The base class is destroying objects with Sub Finalize.")
InBlock.gif         
' Place final cleanup tasks here.
InBlock.gif
         MyBase.Finalize()
ExpandedSubBlockEnd.gif      
End Sub

ExpandedBlockEnd.gif   
End Class

None.gif
ExpandedBlockStart.gifContractedBlock.gif   
Public Class ThisClassClass ThisClass
InBlock.gif      
Inherits BaseClass
InBlock.gif      
Implements IDisposable ' Implements the Dispose method of IDisposable.
InBlock.gif
      Private m_PropertyValue  As Integer
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Sub New()Sub New(ByVal SomeValue As Integer)
InBlock.gif         
MyBase.New() ' Call MyBase.New if this is a derived class.
InBlock.gif
         MsgBox("Sub New is initializing the class ThisClass.")
InBlock.gif         
' Place initialization statements here. 
InBlock.gif
         m_PropertyValue  = SomeValue
ExpandedSubBlockEnd.gif      
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Property ThisProperty()Property ThisProperty() As Integer
InBlock.gif         
Get
InBlock.gif            ThisProperty 
= m_PropertyValue 
InBlock.gif         
End Get
InBlock.gif         
Set(ByVal Value As Integer)
InBlock.gif            m_PropertyValue  
= Value
InBlock.gif         
End Set
ExpandedSubBlockEnd.gif      
End Property

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Sub DoSomething()Sub DoSomething()
InBlock.gif         
' Place code here that does some task.
ExpandedSubBlockEnd.gif
      End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Protected Overrides Sub Finalize()Sub Finalize()
InBlock.gif         
MsgBox("Finalize is destroying objects in ThisClass.")
InBlock.gif         
' Place final cleanup tasks here.
InBlock.gif
         MyBase.Finalize()
ExpandedSubBlockEnd.gif      
End Sub

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif      
Overridable Sub Dispose()Sub Dispose() Implements IDisposable.Dispose
InBlock.gif         
MsgBox("The ThisClass is running Dispose.")
ExpandedSubBlockEnd.gif      
End Sub

ExpandedBlockEnd.gif   
End Class

当运行此示例时,ThisClass 类调用 BaseClass 类的 Sub New 构造函数。在基类中的构造函数完成以后,ThisClass 类运行 Sub New 中剩余的语句,这些语句初始化 ThisProperty 属性的值。

当不再需要该类时,在 ThisClass 中调用 Dispose 析构函数。

如果最初是从窗体创建 ThisClass 的实例,则在关闭该窗体之前似乎什么都没有发生。Finalize 析构函数此时在 ThisClass 类中运行,最后将在 BaseClass 类中运行。

转载于:https://www.cnblogs.com/zman/archive/2006/07/25/458903.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值