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

页面导航:首页 > 软件编程 > vb.net > VB.Net中文教程(2) 继承与封装性(2)

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

来源: 作者: 时间:2016-02-05 12:32 【

.......#End Region Protected Sub Form1_Click( ByVal sender As Object,&n

 

 

.......

#End Region

   Protected Sub Form1_Click( ByVal sender As Object,

                           ByVal e As System.EventArgs)

       Dim crag As New Teacher("Crag", 38, 45000)

       crag.modifyName("Crag Clinton")

       MessageBox.Show( "AGE: " + str(crag.age) + ",     SALARY: "

                        + str(crag.salary))

   End Sub

End Class

 

因为name是Person类别的私有资料,在子类别Teacher的modifyName()里不能使用此资料名称,所以错了。如果将Person类别定义为﹕

 

ex03.bas

Imports System.ComponentModel

Imports System.Drawing

Imports System.WinForms

---------------------------------------------------------

Class Person

   Protected name As String

   Public age As Integer

 

   Public Sub New(ByVal na As String, ByVal a As Integer)

       name = na

       age = a

   End Sub

   Public Function GetName() As String

       GetName = name

   End Function

End Class

 

Class Teacher

   Inherits Person

  

   Public salary As Single

   Public Sub New(ByVal na As String, ByVal a As Integer, ByVal sa As Single)

       MyBase.New(na, a)

       salary = sa

   End Sub

   Public Sub modifyName(ByVal na As String)

       name = na

   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 "

   

Tags:

文章评论


<