打印本文 打印本文 关闭窗口 关闭窗口
VB.Net中文教程(4) 类别继承(Inheritance)关系
作者:武汉SEO闵涛  文章来源:敏韬网  点击数4675  更新时间:2009/4/23 19:01:08  文章录入:mintao  责任编辑:mintao
          no As Integer)
        SetValue(na, a)
        student_number = no
    End Sub
    Public Sub pr()
        MyBase.Display()
        Messagebox.Show("StudNo: " + str(student_number))
    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 x As New Person()
        x.SetValue("Alvin", 32)
        Dim y As New Student()
        y.SetValue("Tom", 36, 11138)
        x.Display()
        y.pr()
    End Sub
End Class

此程序输出﹕
             Name: Alvin    Age: 32
             Name: Tom      Age: 36
             StudNo: 11138

    此时﹐Student 类别含有name、age 及student_number三个资料成员。而且拥有 SetValue()、Person.SetValue() 、Display()、pr() 及birthYear()五个程序成员。于是建立了如下之继承关系﹕
                  
                  
    x 对象含name及age 两项资料﹐指令──x.SetValue ("Alvin", 32)﹐将资料存入x 对象中。因Student继承Person﹐所以y 对象内含name、age 及student_number三项资料﹐指令──y.SetValue("Tom", 36, 11138) 将资料存入y 对象中。上述 SetValue()程序之功能是﹕设定对象之初值。可由建构者(Constructor) 代替之。所以此Student之定义相当于──

''''ex06.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms

''''----------------------------------------------------
Public Class Person
    Private name As String
    Private age As Integer
    Public Sub New()
    End Sub
    Public Sub SetValue(ByVal na As String, ByVal a As Integer)
        name = na
        age = a
    End Sub
    Public Function birthDay() As Integer
        birthDay = 2001 - age
    End Function
    Public Sub Display()
        Messagebox.Show("Name: " + name + "   Age: " + str(age))
    End Sub
End Class

Public Class Teacher
    Inherits Person
   
    Private salary As Decimal
    Public Overloads Sub SetValue(ByVal na As String, ByVal a As Integer, ByVal sa As Decimal)
        SetValue(na, a)
        salary = sa
    End Sub
    Public Sub pr()
        MyBase.Display()
        Messagebox.Show("Salary: " + str(salary))
    End Sub
End Class

Public Class Student
    Inherits Person
   
    Private student_number As Integer
    Public Sub New(ByVal na As String, ByVal a As Integer, ByVal no As Integer)
        SetValue(na, a)
        student_number = no
    End Sub
    Public Sub pr()
        MyBase.Display()
        Messagebox.Show("StudNo: " + str(student_number))
    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 x As New Person()
        x.SetValue("Alvin", 32)
        Dim y As New Student("Tom", 36, 11138)
        x.Display()
        y.pr()
    End Sub
End Class

此程序输出﹕
                 Name: Alvin    Age: 32
                 Name: Tom      Age: 36
                 StudNo: 11138

子类别之建构者呼叫父类别之SetValue()来设定对象初值。请仔细看y 对象之诞生过程﹕y 诞生时﹐建构者──

    Public Sub New(ByVal na As String, ByVal a As Integer, ByVal no As Integer)
           SetValue(na, a)
           student_number = no
    End Sub

它先呼叫父类别的自然建构者Person.New()一起诞生y 对象。此时﹐Person.New()诞生如下﹕
                      
继承部分是Person.New()诞生的。Person.New()任务完成了﹐轮到Student的建构者本身诞生(扩充)如下﹕

              

    诞生完毕﹐开始执行Student建构者内之指令以设定y 之初值。首先呼叫父类别的SetValue() ﹐于是SetValue()将值存入y 如下﹕

                    
                       
最后﹐执行指令── student_number = no﹐设定student_number数据之初值﹕
                    
                     

于是﹐Student的建构者诞生y 对象﹐也设定初值﹐功成圆满。
    上述的Student建构者呼叫父类别之自然建构者(因为Person类别未定义建构者﹐计算机自动产生自然建构者)来协助诞生对象。如果Person类别定义了建构者﹐Student建构者便能直接呼叫Person建构者了。例如﹕

''''ex07.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
''''-----------------------------------------------------------------------------------------
Public Class Person
    Private name As String
    Private age As Integer
    Public Sub New(ByVal na As String, ByVal a As Integer)
        name = na
        age = a
    End Sub
    Public Function birthDay() As Integer
        birthDay = 2001 - age
    End Function
    Public Sub Display()
        Messagebox.Show("Name: " + name + "   Age: " + str(age))
    E

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

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