51
chiangho 1 第 6 第 – 第第 大大 6.1 大大 6.2 大大大 (Modules), 大大 ( Classes) and 大大 (Procedures) 6.3 Sub 大大 (Procedures) 6.4 Function 大大 (Procedures) 6.5 大大 (Methods) 6.6 Option 大大大 (Strict) 大 大大大大大大大 6.7 大大大大大大大大 6.8 大大大大 : Pass-by-Value vs. Pass-by-Reference 6.9 大大 6.10 大大大大 : 大大大大 The Fibonacci Series 6.11 大大大大大

Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Embed Size (px)

Citation preview

Page 1: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

1

第 6 章 – 程序大綱6.1 介紹6.2   一般模組 (Modules), 類別 ( Classes) and 程序(Procedures)6.3   Sub 程序 (Procedures)6.4   Function 程序 (Procedures)6.5   方法 (Methods)6.6   Option 的宣告 (Strict) 和 資料型態的轉換6.7 值型態及參照型態 6.8   傳遞參數 : Pass-by-Value vs. Pass-by-Reference6.9 遞迴6.10   遞迴例子 : 費氏數列 The Fibonacci Series6.11   遞迴與重複

Page 2: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

2

大綱6.12 多載程序和選項參數 (Procedure Overloading and Optional Arguments6.13   一般模組 (Modules)

Page 3: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

3

(6-1) 介 紹

• 程序為程式的基本單位• Vb.net 程序分為 sub ,function, property,event

四種# 物件導向程式中 sub, 及 function 為方法 property 為屬性

Page 4: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

4

6.2 模組類別及程序

• Framework 類別函式庫– 提供豐富的類別及方法去執行操作

• 數學操作• 字串操作• 字元操作• 輸入輸出操作• 錯誤檢查

Page 5: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

5

介紹

Page 6: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

6

6.2 模組類別及程序

• 程式設計者自訂的程序– Foundation class(fcl) 無法提供所有程式使用 – 四種程序

• Sub 程序• Function 程序• Property 程序• Event 程序

– 程序觸發是由程序呼叫

Page 7: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

7

6.2 模組類別及程序

Fig. 6.1 模組階層關析

經理

員工 1 員工 2 員工 3

員工 4 員工 5

Page 8: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

8

6.2 模組類別及程序

• 軟體以程序為基本單位– 為何把程式劃分為程序

• 將程式分為程序易於在程式發展中管理• 程式易於重複使用• 避免重複使用的程式碼

Page 9: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

9

6.2 模組類別及程序

• 早期的的程式只有一個稱為 fcl 方法的程序• 隨後 程式中包含自訂的程序

Page 10: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱10

Payment.vb

Program Output

1 ' Fig. 6.2: Payment.vb2 ' Sub procedure that prints payment information.3 4 Module modPayment5 6 Sub Main()7 8 ' call Sub procedure PrintPay 4 times9 PrintPay(40, 10.5)10 PrintPay(38, 21.75)11 PrintPay(20, 13)12 PrintPay(50, 14)13 14 Console.ReadLine() ' prevent window from closing15 End Sub ' Main16 17 ' print amount of money earned in command window18 Sub PrintPay(ByVal hours As Double, ByVal wage As Decimal)19 20 ' pay = hours * wage21 Console.WriteLine("The payment is {0:C}", hours * wage)22 End Sub ' PrintPay23 24 End Module ' modPayment

The payment is $420.00The payment is $826.50The payment is $260.00The payment is $700.00

Main 程序呼叫 printpay程序

PrintPay 形式引數 hours and wage

接收所有實際引數

所有的程序必須在模組或類別中定義 ( PrintPay 在 modPayment. )

Page 11: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

11

6.3 Sub 程序

• 程序定義的格式Sub 程序名稱 ( 參數列 )

宣告及敘述End Sub

• 程序標頭– sub

• 程序名稱 緊跟 sub 關鍵字

– 為一識別字 – 程式的 sub 程序

• 程序主體– 宣告及敘述

Page 12: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

12

6.4 Function 程序

• 與 sub 類似• 與 sub 差異

– Function 呼叫會傳回一個值 ( 代表函數 )sub 不傳回值

Page 13: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱13

SquareInteger.vb

1 ' Fig. 6.3: SquareInteger.vb2 ' Function procedure to square a number.3 4 Module modSquareInteger5 6 Sub Main()7 Dim i As Integer ' counter8 9 Console.WriteLine("Number" & vbTab & "Square" & vbCrLf)10 11 ' square numbers from 1 to 1012 For i = 1 To 1013 Console.WriteLine(i & vbTab & Square(i))14 Next15 16 End Sub ' Main17 18 ' Function Square is executed19 ' only when the function is explicitly called.20 Function Square(ByVal y As Integer) As Integer21 Return y ^ 222 End Function ' Square23 24 End Module ' modSquareInteger

The For structure displays the results of squaring the Integers from 1-10 Square(i) 呼叫

square

Return 敘述傳回一個值且終結程式的執行

Page 14: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱14

SquareInteger.vb

輸出

Number Square 1 12 43 94 165 256 367 498 649 8110 100

Page 15: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

15

6.4 Function 程序

程序定義的格式 Function 程式名稱 ( 參數列 ) As 傳回值的資料型態宣告及敘述End Function

傳回值的資料型態– 傳回值的資料型態

• Return expression- 可在 function 內任一位置– 傳回一個值– 跳回程序呼叫點

Page 16: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

16

6.5 方法

• Definition of method– A 在類別中的程序含 sub 及 function

• FCL 方法• 自訂類別中的方法

Page 17: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱17

Maximum.vb

1 ' Fig. 6.4: Maximum.vb2 ' Program finds the maximum of three numbers input.3 4 Public Class FrmMaximum5 Inherits System.Windows.Forms.Form6 7 ' prompts for three inputs8 Friend WithEvents lblOne As System.Windows.Forms.Label9 Friend WithEvents lblTwo As System.Windows.Forms.Label10 Friend WithEvents lblThree As System.Windows.Forms.Label11 12 ' displays result13 Friend WithEvents lblMaximum As System.Windows.Forms.Label14 15 ' read three numbers16 Friend WithEvents txtFirst As System.Windows.Forms.TextBox17 Friend WithEvents txtSecond As System.Windows.Forms.TextBox18 Friend WithEvents txtThird As System.Windows.Forms.TextBox19 20 ' reads inputs and calculate results21 Friend WithEvents cmdMaximum As System.Windows.Forms.Button22 23 ' Visual Studio .NET generated code24 25 ' obtain values in each text box, call procedure Maximum26 Private Sub cmdMaximum_Click(ByVal sender As System.Object, _27 ByVal e As System.EventArgs) Handles cmdMaximum.Click28

宣告在圖形介面中所使用的控制項在工具箱中產生可見元件

所有表單均繼承 System.Windows.Forms.Form 類別

事件捕捉 cmdMaximum_Click ( 捕捉 )Handles 按鈕 ( cmdMaximum)的clicked 事件

Page 18: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱18

Maximum.vb

輸出

29 Dim value1, value2, value3 As Double30 31 value1 = txtFirst.Text32 value2 = txtSecond.Text33 value3 = txtThird.Text34 35 lblMaximum.Text = Maximum(value1, value2, value3)36 End Sub ' cmdMaximum_Click37 38 ' find maximum of three parameter values39 Function Maximum(ByVal valueOne As Double, _40 ByVal valueTwo As Double, ByVal valueThree As Double)41 42 Return Math.Max(Math.Max(valueOne, valueTwo), valueThree)43 End Function ' Maximum44 45 End Class ' FrmMaximum

從文書盒的 text 屬性取的值

以程序名稱呼叫方法呼叫 fcl 的函數必須參照類別名稱及點運算子(.)

Page 19: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

19

6.5 方法

Fig. 6.5Visual Studio .NET IDE 的 . 參數資訊視窗

參數資訊視窗

Page 20: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

20

6.5 方法

Fig. 6.6 IntelliSense Visual Studio .NET IDE. 的 IntelliSense

Page 21: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

21

6.5 方法 (method)

Fig. 6.7 Math class methods.

方法 描述 例子

Abs(x) 傳回絕對值 Abs(23.7) is 23.7 Abs(0) is 0 Abs(-23.7) is 23.7

Ceiling(x) rounds x to the smallest integer not less than x

Ceiling(9.2) is 10.0 Ceiling(-9.8) is -9.0

Cos(x) 傳回三角函數cosine值 Cos(0.0) is 1.0

Exp(x) 傳回指數值 Exp(1.0) is approximately

2.71828182845905 Exp(2.0) is approximately

7.38905609893065 Floor(x) rounds x to the largest integer

not greater than x Floor(9.2) is 9.0 Floor(-9.8) is -10.0

Log(x) returns the natural logarithm of x

(base e) Log(2.7182818284590451) is approximately 1.0 Log(7.3890560989306504)

is approximately 2.0 Max(x, y) returns the larger value of x and

y (also has versions for Single, Integer and

Long values)

Max(2.3, 12.7) is 12.7 Max(-2.3, -12.7) is -2.3

Min(x, y) returns the smaller value of x and y (also has versions for Single, Integer and

Long values)

Min(2.3, 12.7) is 2.3 Min(-2.3, -12.7) is -12.7

Pow(x, y) calculates x raised to power y

(xy)

Pow(2.0, 7.0) is 128.0 Pow(9.0, .5) is 3.0

Page 22: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

22

6.5 方法

Fig. 6.7 Math class methods.

Sin(x) 傳回三角函數sine值 Sin(0.0) is 0.0

Sqrt(x) 傳回開平方根值 Sqrt(9.0) is 3.0 Sqrt(2.0) is

1.4142135623731 Tan(x) 傳回三角函數tan值s) Tan(0.0) is 0.0

Fig. 6.7 Math 類別的方法.

Page 23: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

23

6.6 Option Strict and Data-Type 轉換

• Option Explicit– On 為預設值– 強迫所有的變數使用前必須宣告

• Option strict– Off 為預設值– 若為 on 則強迫變數必須宣告變數資料型態

• Class Convert– 類別轉換的方法明確轉換資料型態

Page 24: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

24

6.7 值資料型態及參照型態

Fig. 6.9 Property Pages dialog with Option Strict set to On.

Page 25: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

25

6.7 值資料型態及參照型態

• 帶有值得變數資料型態– 包含實際值得變數– 使用於單一值

• 整數• 被精確浮點數

• 參照變數– 包含一個記憶體所在的位置– 已知的物件

• 尾隨字 ( 訂資料型態 )– 在程式中直接訂值得資料型態– 為相對應一原形資料型態

Page 26: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

26

6.7 值資料型態及參照型態

Fig. 6.10 Visual Basic primitive data types.

資料型態 大小 值 標準

Boolean 16 True or False

Char 16 One Unicode character (Unicode character set)

Byte 8 0 to 255 Date 64 1 January 0001 to 31 December 9999

0:00:00 to 23:59:59

Decimal 128 1.0E-28 to 7.9E+28 Short 16 –32,768 to 32,767 Integer 32 –2,147,483,648 to

2,147,483,647

Long 64 –9,223,372,036,854,775,808 to

9,223,372,036,854,775,807

Single 32 1.5E-45 to 3.4E+38 (IEEE 754 floating point)

Double 64 5.0E–324 to 1.7E+308 (IEEE 754 floating point)

Object 32 Data of any type

String 0 to ~2000000000 Unicode characters (Unicode character set)

Fig. 6.10 Visual Basic .net 原 型資料型態

Page 27: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

27

6.7 值型態及參照轉換

資料型態 代表字元 例子

Char c "u"c

Single F 9.802E+31F Double R 6.04E-187R Decimal D 128309.76D Short S 3420S Integer I -867I Long L 19235827493259374L Fig. 6.11 尾隨字

Fig. 6.11 尾隨字 .

Page 28: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

28

6.8 傳遞參數 : Pass-by-Value vs. Pass-by-Reference

• ( 傳值呼叫 )Pass-by-value– 程式呼叫程序時會複製一實際引數值並傳給被呼叫程序

• ( 傳參照呼叫 ) Pass-by-reference

– 呼叫敘述呼叫被呼叫程序時傳遞參照讓被呼叫程序能存取及修正呼叫敘述的原始資料

*visual basic.net ( 沒有傳址呼叫 )

Page 29: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱29

ByRefTest.vb

1 ' Fig. 6.12: ByRefTest.vb2 ' Demonstrates passing by reference.3 4 Module modByRefTest5 6 ' squares three values ByVal and ByRef, displays results7 Sub Main()8 Dim number1 As Integer = 29 10 Console.WriteLine("Passing a value-type argument by value:")11 Console.WriteLine("Before calling SquareByValue, " & _12 "number1 is {0}", number1)13 SquareByValue(number1) ' passes number1 by value14 Console.WriteLine("After returning from SquareByValue, " & _15 "number1 is {0}" & vbCrLf, number1)16 17 Dim number2 As Integer = 218 19 Console.WriteLine("Passing a value-type argument" & _20 " by reference:")21 Console.WriteLine("Before calling SquareByReference, " & _22 "number2 is {0}", number2)23 SquareByReference(number2) ' passes number2 by reference24 Console.WriteLine("After returning from " & _25 "SquareByReference, number2 is {0}" & vbCrLf, number2)26 27 Dim number3 As Integer = 228

實際引數 number1 被傳遞 , 其值被複製一份傳給 squarebyvalue 程序

number2 的參照被傳遞

Page 30: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱30

ByRefTest.vb

29 Console.WriteLine("Passing a value-type argument" & _30 " by reference, but in parentheses:")31 Console.WriteLine("Before calling SquareByReference " & _32 "using parentheses, number3 is {0}", number3)33 SquareByReference((number3)) ' passes number3 by value34 Console.WriteLine("After returning from " & _35 "SquareByReference, number3 is {0}", number3)36 37 End Sub ' Main38 39 ' squares number by value (note ByVal keyword)40 Sub SquareByValue(ByVal number As Integer)41 Console.WriteLine("After entering SquareByValue, " & _42 "number is {0}", number)43 number *= number44 Console.WriteLine("Before exiting SquareByValue, " & _45 "number is {0}", number)46 End Sub ' SquareByValue47 48 ' squares number by reference (note ByRef keyword)49 Sub SquareByReference(ByRef number As Integer)50 Console.WriteLine("After entering SquareByReference" & _51 ", number is {0}", number)52 number *= number53 Console.WriteLine("Before exiting SquareByReference" & _54 ", number is {0}", number)55 End Sub ' SquareByReference56 57 End Module ' modByRefTest

ByVal 指出以傳值傳遞

ByRef 指出以傳參照傳遞

強制運算子強制傳值方式

Page 31: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱31

輸出

Passing a value-type argument by value:Before calling SquareByValue, number1 is 2After entering SquareByValue, number is 2Before exiting SquareByValue, number is 4After returning from SquareByValue, number1 is 2 Passing a value-type argument by reference:Before calling SquareByReference, number2 is 2After entering SquareByReference, number is 2Before exiting SquareByReference, number is 4After returning from SquareByReference, number2 is 4 Passing a value-type argument by reference, but in parentheses:Before calling SquareByReference using parentheses, number3 is 2After entering SquareByReference, number is 2Before exiting SquareByReference, number is 4After returning from SquareByReference, number3 is 2

Page 32: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱32

Scoping.vb

1 ' Fig. 6.13: Scoping.vb2 ' Demonstrates scope rules and instance variables.3 4 Public Class FrmScoping5 Inherits System.Windows.Forms.Form6 7 Friend WithEvents lblOutput As System.Windows.Forms.Label8 9 ' Windows Form Designer generated code10 11 ' instance variable can be used anywhere in class12 Dim value As Integer = 113 14 ' demonstrates class scope and block scope15 Private Sub FrmScoping_Load(ByVal sender As System.Object, _16 ByVal e As System.EventArgs) Handles MyBase.Load17 18 ' variable local to FrmScoping_Load hides instance variable19 Dim value As Integer = 520 21 lblOutput.Text = "local variable value in" & _22 " FrmScoping_Load is " & value23 24 MethodA() ' MethodA has automatic local value25 MethodB() ' MethodB uses instance variable value26 MethodA() ' MethodA creates new automatic local value27 MethodB() ' instance variable value retains its value28 29 lblOutput.Text &= vbCrLf & vbCrLf & "local variable " & _30 "value in CScoping_Load is " & value31 End Sub ' FrmScoping_Load32 33 ' automatic local variable value hides instance variable34 Sub MethodA()35 Dim value As Integer = 25 ' initialized after each call

Automatic variable value is destroyed when MethodA terminates

Page 33: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱33

Scoping.vb

36 37 lblOutput.Text &= vbCrLf & vbCrLf & "local variable " & _38 "value in MethodA is " & value & " after entering MethodA"39 value += 140 lblOutput.Text &= vbCrLf & "local variable " & _41 "value in MethodA is " & value & " before exiting MethodA"42 End Sub ' MethodA43 44 ' uses instance variable value45 Sub MethodB()46 lblOutput.Text &= vbCrLf & vbCrLf & "instance variable" & _47 " value is " & value & " after entering MethodB"48 value *= 1049 lblOutput.Text &= vbCrLf & "instance variable " & _50 "value is " & value & " before exiting MethodB"51 End Sub ' MethodB52 53 End Class ' FrmScoping

變數 值為 (line 12 的外部變數 ) .

Page 34: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

34

6.9 遞迴

• 遞迴程序 (ecursive procedure )– 遞迴可直接或間接遞迴– 解決事情的一種演算法– 遞迴程序可看出演算法中解決事情的最容易的例子– 對於複雜的問題可利用遞迴由簡化程序– 遞迴呼叫

• 自我呼叫程序

Page 35: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

35

6.9 遞迴 ( 階層 )

Fig. 6.18 遞迴呼叫 5!.

5!

5 * 4!

4 * 3!

3 * 2!

2 * 1!

1

5!

5 * 4!

4 * 3!

3 * 2!

2 * 1!

1

Final value = 120

5! = 5 * 24 = 120 is returned

4! = 4 * 6 = 24 is returned

3! = 3 * 2 = 6 is returned

2! = 2 * 1 = 2 is returned

1 returned

(a) 遞迴呼叫程序 (b) 每一遞迴的傳回值

Page 36: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱36

Factorial.vb

1 ' Fig. 6.19: Factorial.vb2 ' Calculating factorials using recursion.3 4 Public Class FrmFactorial5 Inherits System.Windows.Forms.Form6 7 Friend WithEvents lblEnter As Label ' prompts for Integer8 Friend WithEvents lblFactorial As Label ' indicates output9 10 Friend WithEvents txtInput As TextBox ' reads an Integer11 Friend WithEvents txtDisplay As TextBox ' displays output12 13 Friend WithEvents cmdCalculate As Button ' generates output14 15 ' Visual Studio .NET generated code16 17 Private Sub cmdCalculate_Click(ByVal sender As System.Object, _18 ByVal e As System.EventArgs) Handles cmdCalculate.Click19 20 Dim value As Integer = Convert.ToInt32(txtInput.Text)21 Dim i As Integer22 Dim output As String23 24 txtDisplay.Text = ""25 26 For i = 0 To value27 txtDisplay.Text &= i & "! = " & Factorial(i) & vbCrLf28 Next29 30 End Sub ' cmdCalculate_Click

轉換字串成整數

Page 37: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱37

Factorial.vb

31 32 ' recursively generates factorial of number33 Function Factorial(ByVal number As Long) As Long34 35 If number <= 1 Then ' base case36 Return 137 Else38 Return number * Factorial(number - 1)39 End If40 41 End Function ' Factorial42 43 End Class ' FrmFactorial

If number 大於 1 則執行遞迴呼叫

若沒有結束條件傳回值則會發生錯誤

Page 38: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

38

6.10 遞迴 : 費氏數列 ( Fibonacci Series)

• 費氏數列– 前二項為 0 ,1 fib(0)=0,fib(1)=1

– 其他項為其前二項之和 fib(n)=fib(n-1)+fib(n-2)

• 黃金比例– 費氏數列會收斂成黃金比例 1.618

• 費氏數列– 每一項為為其前二項之和 – 如第 30 項為 2,692,537

Page 39: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱39

Fibonacci.vb

1 ' Fig. 6.20: Fibonacci.vb2 ' Demonstrating Fibonacci sequence recursively.3 4 Public Class FrmFibonacci5 Inherits System.Windows.Forms.Form6 7 Friend WithEvents lblPrompt As Label ' prompts for input8 Friend WithEvents lblResult As Label ' displays result9 10 Friend WithEvents cmdCalculate As Button ' calculates result11 12 Friend WithEvents txtInputBox As TextBox ' reads an Integer13 14 ' Visual Studio .NET generated code15 16 ' displays Fibonacci number in txtInputBox17 Private Sub cmdCalculate_Click(ByVal sender As System.Object, _18 ByVal e As System.EventArgs) Handles cmdCalculate.Click19 20 ' read input21 Dim number As Integer = Convert.ToInt32(txtInputBox.Text)22 23 lblResult.Text = "Fibonacci Value is " & Fibonacci(number)24 End Sub ' cmdCalculate_Click25 26 ' calculate Fibonacci value recusively 27 Function Fibonacci(ByVal number As Integer) As Long28 29 ' check for base cases30 If number = 1 OrElse number = 0 Then31 Return number32 Else33 Return Fibonacci(number - 1) + Fibonacci(number - 2)34 End If35

非遞迴

遞迴呼叫

Page 40: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱40

Fibonacci.vb

36 End Function ' Fibonacci37 38 End Class ' FrmFibonacci

Page 41: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

41

6.11 費氏數列的遞迴呼叫

Fig. 6.21 費氏數列的遞迴呼叫

Fibonacci( 3 )

Fibonacci( 2 ) Fibonacci( 1 )

Fibonacci( 0 )Fibonacci( 1 ) return 1

return

return 1 return 0

return

Page 42: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

42

6.11 遞迴與重複

• 重複 (Iteration)– 重複結構

• For, While or Do/Loop Until

• 遞迴– 遞迴程序呼叫– 使用選擇結構

• If/Then, If/Then/Else or Select

• 遞迴呼叫浪費執行時間 ( 程序呼叫控制權轉移 )及記憶體

Page 43: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

43

6.12 程序過載 (overloading) 及選項參數

• 過載 (Overloading)– 同名的程序但參數的個數或資料型態不依樣– 可增加程式的可讀式

• 選項參數 (Optional arguments)– 呼叫程序時才決定傳遞的參數

Page 44: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱44

Overload.vb

1 ' Fig. 6.22: Overload.vb2 ' Using overloaded methods.3 4 Public Class FrmOverload5 Inherits System.Windows.Forms.Form6 7 Friend WithEvents outputLabel As Label8 9 ' Visual Studio .NET generated code10 11 Private Sub FrmOverload_Load(ByVal sender As System.Object, _12 ByVal e As System.EventArgs) Handles MyBase.Load13 14 outputLabel.Text = "The square of Integer 7 is " & _15 square(7) & vbCrLf & "The square of Double " & _16 "7.5 is " & square(7.5)17 End Sub ' FrmOverload_Load18 19 Function Square(ByVal value As Integer) As Integer20 Return Convert.ToInt32(value ^ 2)21 End Function ' Square22 23 Function Square(ByVal value As Double) As Double24 Return value ^ 225 End Function ' Square26 27 End Class ' FrmOverload

Page 45: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱45

Overload2.vb

1 ' Fig. 6.23: Overload2.vb2 ' Using overloaded procedures with identical signatures and3 ' different return types.4 5 Public Class FrmOverload26 Inherits System.Windows.Forms.Form7 8 Friend WithEvents outputLabel As Label9 10 ' Visual Studio .NET generated code11 12 Private Sub FrmOverload2_Load(ByVal sender As System.Object, _13 ByVal e As System.EventArgs) Handles MyBase.Load14 15 outputLabel.Text = "The square of Integer 7 is " & _16 square(7) & vbCrLf & "The square of Double " & _17 "7.5 is " & square(7.5)18 End Sub ' FrmOverload2_Load19 20 Function Square(ByVal value As Double) As Integer21 Return Convert.ToInt32(value ^ 2)22 End Function ' Square23 24 Function Square(ByVal value As Double) As Double25 Return value ^ 226 End Function ' Square27 28 End Class ' FrmOverload2

Page 46: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱46

Overload2.vb

Program Output

The creating of overloaded procedures with identical parameter lists and different return types produces a syntax error

Page 47: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

chiangho

47

6.13 一般模組 Modules

• 一般模組 Modules– 相關程序行程群組以利重複引用– 與類別引用類似

Page 48: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱48

DiceModule.vb

1 ' Fig. 6.25: DiceModule.vb2 ' A collection of common dice procedures.3 4 Imports System.IO5 6 Module modDice7 8 Dim randomObject As Random = New Random()9 10 ' rolls single die11 Function RollDie() As Integer12 Return randomObject.Next(1, 7)13 End Function ' RollDie14 15 ' die summation procedure16 Function RollAndSum(ByVal diceNumber As Integer) _17 As Integer18 19 Dim i As Integer 20 Dim sum As Integer = 021 22 For i = 1 To diceNumber23 sum += RollDie()24 Next25 26 Return sum27 End Function ' RollAndSum

Page 49: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱49

DiceModule.vb

28 29 ' returns die image30 Function GetDieImage(ByVal dieValue As Integer, _31 Optional ByVal baseImageName As String = "die") _32 As System.Drawing.Image33 34 Return Image.FromFile( _35 Directory.GetCurrentDirectory & _36 "\Images\" & baseImageName & dieValue & ".png")37 End Function ' GetDieImage38 39 End Module ' modDice

Page 50: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱50

DiceModuleTest.vb

1 ' Fig. 6.26: DiceModuleTest.vb2 ' Demonstrates modDiceModule procedures3 4 Imports System.Drawing5 6 Public Class FrmDiceModuleTest7 Inherits System.Windows.Forms.Form8 9 Friend WithEvents lblSum As Label ' displays 10-roll sum10 11 Friend WithEvents diceGroup As GroupBox12 13 ' dice images14 Friend WithEvents picDie1 As PictureBox15 Friend WithEvents picDie2 As PictureBox16 17 Friend WithEvents cmdRollDie1 As Button ' rolls blue die18 Friend WithEvents cmdRollTen As Button ' simulates 10 rolls19 Friend WithEvents cmdRollDie2 As Button ' rolls red die20 21 ' Visual Studio .NET generated code22 23 Private Sub cmdRollDie1_Click(ByVal sender As System.Object, _24 ByVal e As System.EventArgs) Handles cmdRollDie1.Click25 26 picDie1.Image = modDice.GetDieImage(modDice.RollDie())27 End Sub ' cmdRollDie1_Click28 29 Private Sub cmdRollDie2_Click(ByVal sender As System.Object, _30 ByVal e As System.EventArgs) Handles cmdRollDie2.Click31 32 picDie2.Image = modDice.GetDieImage(modDice.RollDie(), _33 "redDie")34 End Sub ' cmdRollDie2_Click35

Page 51: Chiangho 1 第 6 章 – 程序 大綱 6.1 介紹 6.2 一般模組 (Modules), 類別 ( Classes) and 程序 (Procedures) 6.3 Sub 程序 ( Procedures) 6.4 Function 程序 ( Procedures)

Chiang ho

大綱51

DiceModuleTest.vb

36 Private Sub cmdRollTen_Click(ByVal sender As System.Object, _37 ByVal e As System.EventArgs) Handles cmdRollTen.Click38 39 lblSum.Text = Convert.ToString(modDice.RollAndSum(10))40 End Sub ' cmdRollTen_Click41 42 End Class ' FrmDiceModuleTest