当前位置:编程学习 > VB >>

VB.Net中文教程(2) 继承与封装性(4)

 

 

   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 p1 As New FullTime_Teacher("David", 25)

       p1.Show()

       p1.modifyValue("Kent Smith", 45)

       p1.Show()

   End Sub

End Class

 

此程序输出:

             Name: David         Age: 25

             Name: Kent Smith    Age: 45

 

 

   Show()是Person类别的Public程序,孙子类别FullTime_Teacher继承之,成为FullTime_Teacher类别的Public程序,所以Form1_Click()程序能写着指令:p1.Show()。name和modifyAge()是Person的Protected成员,Teacher的modify()程序里能直接使用它们。而且FullTime_Teacher的modifyData()程序里能直接使用它们。但是Form1_Click()就无法使用它们。modify()是Teacher的Protected成员,FullTime_Teacher的modifyValue()程序里能直接使用它们,但Form1_Click()就不行使用。所以若将上述Form1_Click()的指令改为如下,就不对了:

 

Protected Sub Form1_Click( ..... )

       Dim p1 As New FullTime_Teacher("David", 25)

       p1.Show()

       p1.modify("Kent Smith", 45)   Error!

       p1.modifyAge(24)   Error!

       p1.Show()

   End SubEnd Class

 

 

补充:软件开发 , Vb ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,