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

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

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

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

 

#End Region

   Protected Sub Form1_Click( ByVal sender As Object,

                           ByVal e As System.EventArgs)

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

       p1.Show()

       p1.modify("David Smith", 28)

       p1.Show()

   End Sub

End Class

 

 

此程序输出:

              Name: David         Age: 25

              Name: David Smith    Age: 45

 

   modifyAge()为私有程序﹐New()及modify()为公用程序。modifyAge()为Person类别之程序成员﹐所以modify()能呼叫modifyAge()。Person类别外之函数不能呼叫modifyAge()。例如﹕在Form1_Click()中﹐可写着 p1.modify()﹐因为modify()为公用程序。但于Form1_Click()内﹐就不可写着 p1.modifyAge()﹐因为modifyAge()为 Private函数。如果放宽对modifyAge()之限制﹐使Person之子类别能呼叫modifyAge()﹐就须定义modifyAge()为 Protected函数﹐如下﹕

 

"ex05.bas

Imports System.ComponentModel

Imports System.Drawing

Imports System.WinForms

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

Class Person

   Protected name As String

   Private age As Integer

   Protected 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 Show()

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

   End Sub

End Class

 

Class Teacher

   Inherits Person

  

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

       MyBase.New(na, a)

   End Sub

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

       MyBase.name = na

       MyBase.modifyAge(a)

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

       p1.Show()

       p1.modify("Kent Smith", 45)

       p1.Show()

   End Sub

End Class

 

 

此程序输出:

                Name: David        Age: 25

                Name: Kent Smith    Age: 45

 

此时﹐子孙类别之函数能呼叫modifyAge()﹐但家族外之类别仍不可呼叫它。总结归纳为简单规则﹕

   ◎如果允许任何类别使用﹐就宣告为Public。

   ◎如果允许子类别使用﹐就宣告为Protected 。

   ◎如果允许本类别使用﹐就宣告为Private 。

 请在看个例子:

 

"ex06.bas

Imports System.ComponentModel

Imports System.Drawing

Imports System.WinForms

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

Class Person

   Protected name As String

   Private age As Integer

   Protected 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 Show()

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

   End Sub

End Class

 

Class Teacher

   Inherits Person

  

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

       MyBase.New(na, a)

   End Sub

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

       MyBase.name = na

       MyBase.modifyAge(a)

   End Sub

End Class

 

Class FullTime_Teacher

   Inherits Teacher

  

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

       MyBase.New(na, a)

   End Sub

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

       MyBase.modify(na, a)

   End Sub

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

       MyBase.name = na

       MyBase.modifyAge(a)

   End Sub

End Class

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

Public Class Form1

   Inherits System.WinForms.Form

 

 

此文章不能满足您的需求?那就就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)程序多...

热点文章

更多