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
责任编辑:Lily
| SA空口令破解和保护 |
| 怎样使MySQL更安全? |
| Mysql数据库的安全配置、实用技巧 |
| 如何安全的配置和应用MySQL数据库? |
| Oracle数据库安全策略分析 |
| SQL Server 2000的安全配置 |
| 使一个新的MySQL安装更安全 |
| 教您如何安全的应用MySQL |
| MySQL数据库安全配置 |
| MySQL安全性指南(1) |
| MySQL安全性指南(2) |

