繁体中文
设为首页
加入收藏
当前位置:在线教程首页 >> 程序设计 >> .Net专区 >> VB.net语言 >> VB.Net中文教程(2) 继承与封装性(2)

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

2005-10-10 05:48:00  作者:未知  来源:教程网  浏览次数:133  文字大小:【】【】【

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

 

.......

#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 "

    ......

#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) + ",     NAME: "

                        + crag.GetName())

   End Sub

End Class

 

此程序输出:

             AGE: 42,   NAME: Crag Clinton

 

   此时﹐name为家族公用之资料﹐凡是Person之子孙类别皆可取用之。但家族外之程序(如 Form1_Click()程序)仍不得直接使用之。如果上述定义改为﹕

 

Class Person

   Protected name As String

   Private salary As Decimal

   Public age As Integer

 

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

       name = na

       age = a

   End Sub

End Class

 

此时﹐salary为类别私有﹐其它类别不得使用。name为家族私有﹐家族外之类别不得使用。age为公用﹐任何类别皆可用。

 

 

1.2 公用与私有程序

   上节介绍过﹕资料成员有 Private、Protected 及Public之分。同样地﹐程序成员也可分为 Private、Protected 及Public。虽然在应用上﹐程序成员大多定义为Public﹐但必要时﹐也能将过程定义为Private 或 Protected。私有程序和私有资料一样﹐只限于该类别之程序才能呼叫它。例如﹕

 

"ex04.bas

Imports System.ComponentModel

Imports System.Drawing

Imports System.WinForms

"----------------------------------------------------------------------

Class Person

   Private name As String

   Private age As Integer

   Private Sub modifyAge(ByVal a As Integer)

       age = a

   End Sub

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

       name = na

       age = a

   End Sub

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

       name = na

       modifyAge(a)

   End Sub

   Public Sub Show()

       Messagebox.Show("Name: " + name + "   Age: " + str(age))

   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 "

     .......

 

 

此文章不能满足您的需求?那就就Google一下吧:)
Google
 

责任编辑:Lily


相关文章
SA空口令破解和保护
怎样使MySQL更安全?
Mysql数据库的安全配置、实用技巧
如何安全的配置和应用MySQL数据库?
Oracle数据库安全策略分析
SQL Server 2000的安全配置
使一个新的MySQL安装更安全
教您如何安全的应用MySQL
MySQL数据库安全配置
MySQL安全性指南(1)
MySQL安全性指南(2)
 

最新文章

更多

· 类别与封装性(1)
· 类别与封装性(2)
· 类别与封装性(3)
· 类别与封装性(4)
· 类别与封装性(5)
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(3)程序多...

推荐文章

更多

· 类别与封装性(1)
· 类别与封装性(2)
· 类别与封装性(3)
· 类别与封装性(4)
· 类别与封装性(5)
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(2) 继承...
· VB.Net中文教程(3)程序多...

热点文章

更多