html5中文学习网

您的位置: 首页 > 网络编程 > ASP.NET » 正文

VB.Net中文教程(1) 类别与封装性_.NET教程_编程技术

[ ] 已经帮助:人解决问题
封装|教程|中文
1. 类别的「程序成员」(Procedure Member)

类别 (Class)之任务是把资料(Data)和程序(Procedure)组织并封装起来。类别告诉计算机「其对象应含有那些资料、应含有那些程序裨处理外界传来之讯息」。类别须详细说明它的资料及程序我们称此资料是类别之「资料成员」(Data Member) 而称此程序是类别之「程序成员」(Procedure Member)。有关类别内容之叙述就是所谓的类别定义(Class Definition)。类别定义之格式为──

类别之用途为宣告对象。例如

'ex01.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'----------------------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height As Single
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 a As New Tree()
MsgBox("Object a Is Created.")
End Sub
End Class

此程序定义了类别Tree它只含资料而无程序为一「阳春型」之类别。当计算机执行到Form1_Click()程序内之宣告指令──
Dim a As New Tree()

就分配足够存放这 3项资料的内存空间给予对象 a。然而此Tree类别只有资料而无程序。所以对象 a无法接受外来之讯息。此时可加入程序成员使Tree类别含有程序、具有动力对象就有能力来处理讯息了。例如

'ex02.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'------------------------------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height As Single
Public Sub input(ByVal hei As Single)
height = hei
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 a As New Tree()
a.input(2.1)
Messagebox.Show("Set a.Height to 2.1", "Hello!")
End Sub
End Class

此程序输出:Set a.Height to 2.1
现在Tree类别已拥有程序成员 input()。程序成员的写法与一般VB程序相同只是它应宣告于类别内成为类别之专属程序。此刻对象 a含有 3项资料及 1个程序

计算机执行到指令──
a.input(2.1)

就将讯息──input(2.1)传给对象 a。此时计算机呼叫并执行对象 a内之input() 程序。对象 a内之 input()就是定义于Tree类别内之input() 于是Form1_Click()就把自变量──2.1 传给 input()内之 hei变量。



接下来叙述──
height = hei

把 hei变量值存入对象 a之资料成员──height中。



此刻对象 a对讯息之处理完成了其内部资料改变了亦即对象 a之内部状态(Internal State)改变了这是对象的行为之一。上述您已经会加入一个程序了依同样方法继续加入其它程序让对象的兴为更多采多姿。例如

'ex03.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'-----------------------------------------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height As Single
Public Sub input(ByVal hei As Single)
height = hei
End Sub
Public Function inquireHeight() As Single
inquireHeight = height
End Function
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 a As Tree = New Tree()
Dim h As Single
a.input(2.1)
h = a.inquireHeight()
Messagebox.Show("height = " + str(h) + "公尺", "HI!")
End Sub
End Class

此程序输出如下height = 2.1公尺
Tree类别有2个程序成员──input() 和inquireHeight()。类别之程序成员必须与其对象配合使用。格式为

亦即必须以讯息之形式出现。例如

如果程序成员不与对象相配合时计算机会如何处理呢例如

'ex04.bas
'Some Error Here !
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'--------------------------------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height As Single
Public Sub input(ByVal hei As Single)
height = hei
End Sub
Public Function inquireHeight() As Single
inquireHeight = height
End Function
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 a As Tree = New Tree()
Dim h As Single
a.input(2.1)
h = inquireHeight()
Messagebox.Show("height = " + str(h) + "公尺", "HI!")
End Sub
End Class

当计算机看到Form1_Click()内之指令──
h = inquireHeight( )

它视inquireHeight()为一独立之程序与Tree类别内之inquireHeight()无关于是计算机去找此inquireHeight()之定义但找不着所以程序错了。因之您要掌握个原则── 程序成员之唯一任务是支持对象之行为必须与对象配合使用。
2. 「封装性」概念

对象把资料及程序组织并「封装」(Encapsulate) 起来只透过特定的方式才能使用类别之资料成员和程序成员。对象如同手提袋只从固定的开口才能存取东西否则您一定不敢把钱放在手提袋中。对象像一座「防火墙」保护类别中的资料使其不受外界之影响。想一想我国的万里长城可保护关内的人民避免受胡人侵犯但长城并非完全封闭而有山海关、玉门关等出入口。对象和万里长城之功能是一致的它保护其资料成员但也有正常的资料存取管道以程序成员来存取资料成员。请看个程序

'ex05.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'-------------------------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height As Single
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 a As New Tree()
a.height = 2.1
Messagebox.Show("height = " + str(a.height) + "公尺")
End Sub
End Class

此程序输出如下height = 2.1公尺
此程序中Tree类别含有 3个资料成员即对象内含有3个资料值,此类别之程序成员能直接存取之。同时,也允许其它程序来存取资料成员之值其存取格式为

例如
a.height = 2.1

此指令把 2.1存入对象 a之height变量中。于是对象 a之内容为



请看个常见错误如下

'ex06.bas
'Some Error Here!
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'-------------------- ------------------------------------
Class Tree
Public varity As String
Public age As Integer
Public height As Single
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 a As New Tree()
height = 2.1
Messagebox.Show("height = " + str(a.height) + "公尺")
End Sub
End Class

Form1_Click()程序内之指令── height = 2.1,此height变量并未与对象配合使用计算机不认为它是Tree类别之height变量。计算机视其为Form1_Click()之自动变量(Automatic Variable)但却未见到它的宣告因之程序错了这是对象对其资料成员保护最松的情形因为对象所属类别(即Tree)之外的程序(如Form1_Click()程序)尚能存取资料成员的内容。就像一颗炸弹除了引信管外尚有许多管道可使炸弹内之化学药品爆炸您将不敢把炸弹摆在飞机上因何时会爆炸将无法控制。同理Tree类别之资料──height变量连外部的Form1_Click()皆可随意改变它那么有一天height之内容出问题了将难以追查出错之缘故这种程序将让您大伤脑筋因为您已无法掌握状况了。
现在的VB程序中能采取较严密之保护措施使您较能控制类别内资料的变化状况。例如

'ex07.bas
'Some Error Here!
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'-----------------------------------------
Class Tree
Private varity As String
Private age As Integer
Private height As Single
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
Public Sub Form1_Click( ByVal sender As Object, ByVal
e As System.EventArgs)
Dim a As New Tree()
a.height = 2.1
MessageBox.Show("height = " + str(a.height))
End Sub
End Class

此程序将原来的Public专用字改为Private对Tree类别之资料成员采取严格之保护措施。Public 与Private之区别为──

Public 表示此类别外之程序可来存取资料成员。
Private 表示此类别外之程序绝无法直接存取资料成员只有程序成员才能存取资料成员。

所以计算机看到指令── a.height = 2.1,因Tree类别采取严格保护措施(private)则Form1_Click()程序不能使用height变量名称。所以指令── a.height = 2.1错了。也许您问道这样岂不是无法存取类别内之资料成员吗答案是「类别内之程序成员(Member Function) 可存取资料成员而类别外之程序能藉程序成员代之存取资料成员。」

图1、类别之沟通管道──程序成员

这如同引信管才能引起炸弹内之化学药品爆炸人们只能经由引信管引爆之让人们觉得使用炸弹既安全又简单。同样地对象经由程序成员和外界沟通可减少外界无意中破坏对象内之资料(无意中引爆炸弹)。例如

'ex08.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'--------------------------------------------------
Class Tree
Private varity As String
Private age As Integer
Private height As Single
Public Sub input(ByVal hei As Single)
height = hei
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 a As New Tree()
a.input(2.1)
MessageBox.Show("OK")
End Sub
End Class

将input()摆在Tree类别中为Tree之程序成员它能存取资料成员height之值。把input()程序宣告为Public表示类别外之程序可藉来呼叫它其呼叫格式为──

简单规则是
Public 表示授权给外界之程序藉由此格式呼叫程序成员。

如果此程序改写为



'ex09.bas
'Some Error Here!
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'----------------------------------------------
Class Tree
Private varity As String
Private age As Integer
Private height As Single
Private Sub input(ByVal hei As Single)
height = hei
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 a As New Tree()
a.input(2.1)
MessageBox("OK")
End Sub
End Class

这程序有问题因为 input()是Tree类别之Private程序成员而非Public程序成员所以不能使用如下格式──



所以此程序错了。 请再看个例子吧

'ex10.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'-------------------------------------------
Class Tree
Private varity As String
Private height As Single
Public age As Integer
Public Sub ShowAge()
MessageBox.Show("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 "
.......
#End Region
Protected Sub Form1_Click( ByVal sender As Object, ByVal
e As System.EventArgs)
Dim a As New Tree()
a.age = 8
a.age = a.age + 2
a.ShowAge()
End Sub
End Class

Tree类别包含 2个Private成员── variety及height且有 2个Public成员── age及 ShowAge()。由于age是Public资料成员所以Fom1_Click()可使用格式──

来存取Tree内之age变量。指令── a.age = 8把8存入对象 a内之age 变量。指令──a.age = a.age + 2使对象a之age变量值加上2成为10。由于ShowAge()程序是Public程序成员也可使用格式──

来呼叫 ShowAge()程序。
由于类别(即对象)之目的是保护资料并且提供程序成员来与外界沟通(接受、处理、并反应讯息)。通常资料成员皆宣告为Private而程序成员皆宣告为Public。亦即尽量少用格式──

而尽量多用格式──

例如

'ex11.bas
Imports System.ComponentModel
Imports System.Drawing
Imports System.WinForms
'--------------------------------------------------------------------
Class Tree
Private varity As String
Private age As Integer
Private height As Single
Public Sub input(ByVal v As String, ByVal a As Integer, ByVal hei As Single)
varity = v
age = a
height = hei
End Sub
Public Sub Show()
MessageBox.Show(varity + ", " + str(age) + ", " + str(height))
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 a, b As New Tree()
a.input("peach", 8, 2.1)
b.input("pinapple", 2, 0.5)
a.Show()
b.Show()
End Sub
End Class

这个VB程序Tree内之资料成员──variety, age及height皆为Private成员,而input()及Show()程序是Public成员。Form1_Click()程序中首先诞生对象── a及 b。接下来指令──



把 3项资料分别存入对象 a之资料成员中a 之内容为



同样地指令──
b.input( "pineapple", 2, 0.5 )

也把 3项资料存入对象 b之中则 b之内容为



最后呼叫Show()程序把对象 a和对象 b之内容显示出来

peach, 8, 2.1
pineapple, 2, .5
n

GJ8HTML5中文学习网 - HTML5先行者学习网
GJ8HTML5中文学习网 - HTML5先行者学习网
(责任编辑:)
推荐书籍
推荐资讯
关于HTML5先行者 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 人才招聘 - 帮助