使用构造函数和析构函数

Myth丶恋晨 2021-11-08 23:40 600阅读 0赞

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

若要为类创建构造函数,请在类定义的任何位置创建名为 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))。

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

![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 类中运行。

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

[ExpandedBlockStart.gif]:

发表评论

表情:
评论列表 (有 0 条评论,600人围观)

还没有评论,来说两句吧...

相关阅读

    相关 构造函数函数

    构造函数和析构函数是面向对象编程中的重要概念,它们在对象的创建和销毁过程中起着关键作用。构造函数用于初始化对象的状态,而析构函数则负责在对象被销毁时执行清理操作。在本文中,我们

    相关 构造函数函数

    构造函数      先看看构造函数的调用顺序规则,只要我们在平时编程的时候遵守这种约定,任何关于构造函数的调用问题都能解决;构造函数的调用顺序总是如下: 1.基类构

    相关 构造函数函数

    构造函数      先看看构造函数的调用顺序规则,只要我们在平时编程的时候遵守这种约定,任何关于构造函数的调用问题都能解决;构造函数的调用顺序总是如下: 1.基类构

    相关 拷贝构造函数函数

    一、拷贝构造函数 (一)、何为拷贝? 拷贝一词来源于英文中的copy,译为:抄写,复制,复制品。将原有物件,按照同样式的在来一份。 \\举个栗子: 饭点到了,和

    相关 C++ 构造函数函数

    百度百科:构造函数 ,是一种特殊的方法。主要用来在创建对象时初始化对象, 即为对象成员变量赋初始值,总与new运算符一起使用在创建对象的语句中。特别的一个类可以有多个构造函数

    相关 使用构造函数函数

    构造函数和析构函数控制对象的创建和毁坏。 若要为类创建构造函数,请在类定义的任何位置创建名为 Sub New 的过程。若要创建参数化构造函数,请像为其他任何过程指定参数那样为