如何:将实现代码转换为新的继承形式
更新:2007 年 11 月
Visual Basic 6.0 使用 Implements 语句进行接口继承。较新版本的 Visual Basic 提供两种继承:一种使用 Implements 语句,另一种使用 Inherits 语句。本主题演示如何使用这两个选项转换代码。
本页以下面的 Visual Basic 6.0 代码为例:
' Account class.
Private mvarBalance As Currency
Private mvarOwner As String
Public Property Let Owner(ByVal vData As String)
mvarOwner = vData
End Property
Public Property Get Owner() As String
Owner = mvarOwner
End Property
Public Function Deposit(ByVal amount As Currency) As Currency
End Function
' Savings class.
Private mvarBalance As Currency
Private mvarOwner As String 'local copy
Public Property Let Owner(ByVal vData As String)
mvarOwner = vData
End Property
Public Property Get Owner() As String
Owner = mvarOwner
End Property
Public Function Deposit(ByVal amount As Currency) As Currency
End Function
使用公共接口进行转换
运行“升级向导”。
说明:
“升级向导”是一个用于将 Visual Basic 6.0 应用程序升级到 Visual Basic 2008 的工具。在 Visual Basic 2008 中打开一个 Visual Basic 6.0 项目时,该向导会自动启动。有关更多信息,请参见 如何:用 Visual Basic 升级向导升级项目。
向导将 Account 类转换为接口 (_Account),然后创建两个实现该接口的类(Account 和 Savings)。
Interface _Account Property Owner() As String Function Deposit(ByVal amount As Decimal) As Decimal End Interface Friend Class Account Implements _Account Private mvarBalance As Decimal Private mvarOwner As String Public Property Owner() As String Implements _Account.Owner Get Owner = mvarOwner End Get Set(ByVal Value As String) mvarOwner = Value End Set End Property Public Function Deposit(ByVal amount As Decimal) _ As Decimal Implements _Account.Deposit End Function End Class Friend Class Savings Implements _Account Private Function Account_Deposit(ByVal amount As Decimal) _ As Decimal Implements _Account.Deposit End Function Private Property Account_Owner() As String _ Implements _Account.Owner Get End Get Set(ByVal Value As String) End Set End Property Public Function AddInterest() As Object End Function End Class
创建和使用 Savings 帐户实例的代码与下面的代码类似。
Dim savings As _Account savings = New Savings savings.Owner = "Jeff Pike"
如果希望 Account 作为基类,则可以修改向导输出。
使用接口和实现进行转换
运行“升级向导”。
删除 Account 类。
将 _Account 类重命名为 Account。
在 Savings 类中,将 _Account 替换为 Account。
Friend Class Savings Implements Account Private Function Account_Deposit(ByVal amount As Decimal) _ As Decimal Implements Account.Deposit End Function Private Property Account_Owner() As String Implements Account.Owner Get End Get Set(ByVal Value As String) End Set End Property Public Function AddInterest() As Object End Function End Class
创建和使用 Savings 帐户实例的代码与下面的代码类似。
Dim savings As Account savings = New Savings savings.Owner = "Jeff Pike"
如果希望 Account 类作为基类,则可以修改向导输出。
使用类和继承进行转换
运行“升级向导”。
删除 _Account 接口。
在 Account 类中,删除 Implements 语句中对 _Account 的引用。
Friend Class Account Private mvarBalance As Decimal Private mvarOwner As String Public Property Owner() As String Get Owner = mvarOwner End Get Set(ByVal Value As String) mvarOwner = Value End Set End Property Public Function Deposit(ByVal amount As Decimal) As Decimal End Function End Class
在 Savings 类中,将 Implements _Account 替换为 Inherits Account。
在 Savings 类中,移除 Owner 和 Deposit 成员。
Friend Class Savings Inherits Account Public Function AddInterest() As Object End Function End Class
创建和使用 Savings 帐户实例的代码与下面的代码类似。
Dim savings As Account savings = New Savings savings.Owner = "Jeff Pike"