国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

VB.Net中文教程(1) 類別與封裝性

2024-07-10 13:01:18
字體:
來源:轉載
供稿:網友
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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 米林县| 新宁县| 勃利县| 林州市| 专栏| 金昌市| 柞水县| 明水县| 广平县| 五莲县| 高唐县| 故城县| 安国市| 巴楚县| 柏乡县| 台江县| 靖远县| 伽师县| 个旧市| 营山县| 西和县| 保山市| 宝清县| 古交市| 夏津县| 青铜峡市| 綦江县| 海晏县| 五寨县| 宝坻区| 麦盖提县| 芮城县| 大荔县| 西畴县| 阿坝县| 香港 | 海阳市| 奈曼旗| 乐山市| 吉木乃县| 安远县|