打印本文 打印本文 关闭窗口 关闭窗口
VB.Net中文教程(4) 类别继承(Inheritance)关系
作者:武汉SEO闵涛  文章来源:敏韬网  点击数4675  更新时间:2009/4/23 19:01:08  文章录入:mintao  责任编辑:mintao
得比Account 类别简单﹐但它总共却含有deposit() 、withdraw()等6 个程序﹐功能比Account 类别强大许多。对于指令──account.deposit(5000) 而言﹐其有两种意义﹕
     I. Saving_Account继承Account ﹐所以Saving_Account具有deposit() 程序﹐可将5000存入account 对象。
     II. Saving_Account为Account 之子类别﹐它能借用(再使用)父类别之deposit() 程序﹐来将5000存入account 对象。

    这两者是一体的两面﹐继承功能让软件师能不断新增子类别﹐不断再使用现有类别之程序﹐来简化子类别之复杂度。继承使得子类别拥有祖父类别之各种功能﹐且再增加新功能﹔再使用使得子类别尽量委托祖父类别来担任工作﹐自己以逸待劳。因而﹐善用继承功能﹐时时心存再使用观念﹐便能设计简单却功能强大的子类别了。而程序员的效率提升了﹐软件之成本下降了﹐此乃OOP 的理想目标之一。兹再扩充一个子类别CD_Account如下﹕

''''ex09.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
''''--------------------------------------------------------------------------------
Class Account
    Private Shared next_number As Integer
   
    Protected acc_no As Integer
    Protected name As String
    Protected balance As Decimal
   
    Shared Sub New()
        next_number = 1000
    End Sub
    Public Sub New(ByVal na As String)
        name = na
        balance = 0
        acc_no = next_number
        next_number = next_number + 1
    End Sub
    Public Sub deposit(ByVal money As Decimal)
        balance = balance + money
    End Sub
    Public Sub withdraw(ByVal money As Decimal)
        balance = balance - money
    End Sub
    Public Sub close()
        acc_no = -1
    End Sub
End Class

Class Saving_Account
    Inherits Account
   
    Private interest_rate As Double
    Public Sub New(ByVal na As String, ByVal rate As Double)
        MyBase.New(na)
        Me.interest_rate = rate
    End Sub
    Public Function comp_interest() As Double
        comp_interest = balance * interest_rate
    End Function
    Public Sub display()
        Messagebox.Show("ACC_NO: " + str(MyBase.acc_no))
        Messagebox.Show("余额: " + str(Me.balance) + "    利息: "
                        + str(comp_interest()))
    End Sub
End Class

Class CD_Account
       Inherits Saving_Account

    Public Sub New(ByVal na As String, ByVal rate As Double, ByRef b As Decimal)
        MyBase.New( na, rate + 0.003 )
        MyBase.deposit(b)
    End Sub
End Class
''''----------------------------------------------------------------------------------
Public Class Form1
    Inherits System.WinForms.Form
   
    Public Sub New()
        MyBase.New()
        Form1 = Me
        ''''This call is required by the Win Form Designer.
        InitializeComponent()
        ''''TODO: Add any initialization after the InitializeComponent() call
    End Sub
    ''''Form overrides dispose to clean up the component list.
    Public Overrides Sub Dispose()
        MyBase.Dispose()
        components.Dispose()
    End Sub
#Region " Windows Form Designer generated code "
     .......
#End Region
    Protected Sub Form1_Click( ByVal sender As Object, ByVal
                            e As System.EventArgs)
        Dim a As CD_Account
        a = New CD_Account("Linda Lin", 0.008, 2500)
        a.display()
        a.close()
    End Sub
End Class

CD_Account除了在定义自己的New()之外,皆继承Saving_Account的成员。请您仔细阅读下述程序,看看跟上一个程序有何微妙差异?

''''ex10.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
''''--------------------------------------------------------------------------------
Class Account
    Private Shared next_number As Integer
   
    Protected acc_no As Integer
    Protected name As String
    Protected balance As Decimal
   
    Shared Sub New()
        next_number = 1000
    End Sub
    Public Sub New(ByVal na As String)
        name = na
        balance = 0
        acc_no = next_number
        next_number = next_number + 1
    End Sub
    Public Sub deposit(ByVal money As Decimal)
        balance = balance + money
    End Sub
    Public Sub withdraw(ByVal money As Decimal)
        balance = balance - money
    End Sub
    Public Sub close()
        acc_no = -1
    End Sub
End Class

Class Saving_Account
    Inherits Account
   
    Private interest_rate As Double
    Public Sub New(ByVal na As String, ByVal rate As Double)
        MyBase.New(na)
        Me.interest_rate = rate
    End Sub
    Public Function comp_interest() As Double
        comp_interest = balance * interest_rate
    End Function
    Public Sub display()
        Messagebox.Show("ACC_NO: " + str(MyBase.acc_no))
        Messagebox.Show("余额: " + str(Me.balance) + "    利息: "
                        + str(comp_interest()))
    End Sub
End Class

Class CD_Account
    Private acc As Saving_Account
    Public Sub New(ByVal na As String, ByVal rate As Double, ByRef b As Decimal)
        acc = New Saving_Account(na, rate + 0.003)
        acc.deposit(b)
    End Sub
    Public Function comp_interest() As Double
        comp_interest = acc.comp_interest()
    End Function
    Public Sub display()
        acc.display()
    End Sub
    Public Sub Close()
        acc.close()
    End Sub
End Class
''''----------------------------------------------------------------------------------
Public Class Form1
    Inherits System.WinForms.Form
   
    Public Sub New()
        MyBase.New()
        Form1 = Me
        ''''This call is required by the Win Form Designer.
        InitializeComponent()
        ''''TODO: Add any initialization after the InitializeComponent() call
    End Sub
    ''''Form overrides dispose to clean up the component list.
    Public Overrides Sub Dispose()
        MyBase.Dispose()
        components.Dispose()
    End Sub
#Region " Windows Form Designer generated code "
     .......
#End Region
    Protected Sub Form1_Click( ByVal sender As Object, ByVal
                            e As System.EventArgs)
        Dim a As CD_Account
        a = New CD_Account("Linda Lin", 0.008, 2500)
        ''''a.deposit(5000)        ''''Error here!!
       

上一页  [1] [2] [3] [4] [5] [6]  下一页

打印本文 打印本文 关闭窗口 关闭窗口