转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 软件开发 >> VB.NET程序 >> 正文
VB.Net中文教程(4) 类别继承(Inheritance)关系         ★★★★

VB.Net中文教程(4) 类别继承(Inheritance)关系

作者:闵涛 文章来源:闵涛的学习笔记 点击数:3692 更新时间:2009/4/23 19:01:08
得比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]  下一页


没有相关教程
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · C语言系列  · VB.NET程序
    · JAVA开发  · Delphi程序
    · 脚本语言
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台