ObjectOriented Programming IC Code Reuse Encapsulation Inheritance Polymorphism
物件導向程式設計 (Object-Oriented Programming) • 軟體IC/程式碼重覆使用( Code Reuse) • 封裝(Encapsulation) – 狀態、行為 – 模擬 • 繼承(Inheritance) • 多型(Polymorphism) 3
程式 Dice. Simulation (1/4) using System; namespace Dice. Simulation { /* * 模擬擲骰子示範類別的宣告與物件的使用 * 3/16/2008 * * 測試規畫: * 六個點數出現次數應該大約相同 */ class Program { static void Main(string[] args) { 4
程式 Dice. Simulation (2/4) int[] count = new int[6]; // 累計點數出現次數 for (int i = 0; i < 6; ++i) { count[i] = 0; } const int N = 12000; // 總擲骰次數 int face. Value; Dice dice = new Dice(); // 擲骰N次 for (int i = 0; i < N; ++i) { dice. Toss(); face. Value = dice. Face. Value; ++count[face. Value-1]; } 5
程式 Dice. Simulation (3/4) // 印出結果 for (int i = 0; i < 6; ++i) { Console. Write. Line(" {0} appears {1} times ", i + 1, count[i]); } } } 6
程式 Dice. Simulation (4/4) public class Dice { int face. Value = 1; Random rand = new Random(); public int Face. Value { get { return face. Value; } set { face. Value = value; } } public void Toss() { face. Value = rand. Next() % 6 + 1; } } } 7
函式呼叫流程 static void Main(string[] args) { public class Dice {. . . dice. Toss(); public void Toss() { } face. Value = rand. Next() % 6 + 1; }. . . } public class Random {. . . public int Next() {. . . } 8
類別(Class)觀念 類別 Dice 資料變數(欄位成員 field) face. Value, rand 方法函式(函式成員 function, 屬性 attribute) Face. Value Toss() 9
堆疊(Stack)與堆積(Heap) Heap . . . Stack 10
物件(Object)觀念 dice 1. face. Value 記憶體地址1 dice 1. rand 函式Toss進入地址 dice 2. file. Name 記憶體地址2 dice 2. rand 函式Toss進入地址 函式成員Toss進入點 13
物件的設定(Assignment) dice 1. face. Value 記憶體地址1 dice 1. rand 函式Toss進入地址 dice 2 記憶體地址2 dice 2 = dice 1; dice 2. file. Name dice 2. rand 函式Toss進入地址 函式成員Toss進入點 14
UML 的類別符號 Dice face. Value : int rand: Random Face. Value Toss() : void 15
練習 1 • 宣告並測試以下類別 Car speed : int driver : string Speed Driver Accelerate() Put. On. Brakes() 16
程式 Calculator. Test (1/6) using System; namespace Calculator. Test { /* 測試類別Calculator以說明函式的使用 * 3/17/2007 */ class Program { static void Main(string[] args) { int op = 0; int operand 1; int operand 2; int result; Calulator calculator = new Calulator(); 17
程式 Calculator. Test (2/6) "); do { Console. Write( "指定運算: 0. 結束; 1. 加; 2. 減; 3. 乘; 4. 除: op = int. Parse(Console. Read. Line()); if (op == 0) break; if (op > 4) continue; Console. Write("輸入第一個數字: "); operand 1 = int. Parse(Console. Read. Line()); Console. Write("輸入第二個數字: "); operand 2 = int. Parse(Console. Read. Line()); switch (op) { 18
程式 Calculator. Test (3/6) case 1: result = calculator. Add(operand 1, operand 2); Console. Write. Line("{0} + {1} = {2} ", operand 1, operand 2, result); break; case 2: result = calculator. Subtract(operand 1, operand 2); Console. Write. Line("{0} - {1} = {2} ", operand 1, operand 2, result); break; case 3: result = calculator. Multiply(operand 1, operand 2); 19
程式 Calculator. Test (4/6) Console. Write. Line("{0} * {1} = {2} ", operand 1, operand 2, result); break; case 4: result = calculator. Divide(operand 1, operand 2); Console. Write. Line("{0} / {1} = {2} ", operand 1, operand 2, result); break; default: Console. Write. Line( "Should not appear this message. Debug!!!"); break; } } while (true); } 20 }
程式 Calculator. Test (5/6) /* * 計算器 * 3/17/2007 */ public class Calulator { public int Add(int a, int b) { int result = a + b; return result; } public int Subtract(int a, int b) { int result = a - b; return result; } 21
程式 Calculator. Test (6/6) public int Multiply(int a, int b) { int result = a * b; return result; } public int Divide(int a, int b) { int result = a / b; return result; } } } 22
函式傳回值 Program. Main() result calculator. Add() result return value 23
程式Swap. Integers (1/2) using System; namespace Swap. Integers { /* 交換兩個整數變數 * 以說明傳值呼叫與傳址呼叫 * skj 4/5/2008 */ class Program { static void Main(string[] args) { int x = 3; int y = 5; Console. Write. Line("x = {0}, y Swap(ref x, ref y); Console. Write. Line("x = {0}, y } = {1}", x, y); 24
程式Swap. Integers (2/2) static void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; } } } 25
程式 Pass. By. Reference. And. Out (1/4) using System; namespace Pass. By. Reference. And. Out { /* * 示範傳址參數與out參數的使用方法 * 3/31/2008 */ class Program { static void Main(string[] args) { double length = 100. 0; Square s = new Square(); s. Side = length; 26
程式 Pass. By. Reference. And. Out (2/4) double area 1 = 0. 0; double perimeter 1 = 0. 0; s. Get. Area. And. Perimeter(ref area 1, ref perimeter 1); Console. Write. Line( "正方形邊長: {0}, 面積: {1}, 周長: {2}", length, area 1, perimeter 1); double area 2; double perimeter 2; s. Get. Area. And. Perimeter. Using. Out(out area 2, out perimeter 2); Console. Write. Line( "正方形邊長: {0}, 面積: {1}, 周長: {2}", length, area 2, perimeter 2); Console. Read. Line(); } } 27
程式 Pass. By. Reference. And. Out (3/4) /* * 正方形 * 3/16/2008 */ public class Square { double a; public double Side { get { return a; } set { a = value; } } 28
程式 Pass. By. Reference. And. Out (4/4) public void Get. Area. And. Perimeter(ref double area, ref double perimeter) { area = Math. Pow(a, 2); perimeter = 4. 0 * a; } public void Get. Area. And. Perimeter. Using. Out( out double area, out double perimeter) { area = Math. Pow(a, 2); perimeter = 4. 0 * a; } } } 29
類別Time hour : int min : int sec : int Set. Time() Get. Time() 31
程式 Using. This (1/4) using System; namespace Using. This { /* * 說明this的使用 * 3/17/2007 */ class Program { static void Main(string[] args) { Time t = new Time(); t. Set. Time(11, 30, 52); 32
程式 Using. This (2/4) int hour; int min; int sec; t. Get. Time(out hour, out min, out sec); Console. Write. Line("現在時間{0} : {1} : {2}", hour, min, sec); Console. Read. Line(); } } /* * 以時, 分, 秒表示時間 */ 33
程式 Using. This (3/4) public class Time { int hour; int min; int sec; public void Set. Time(int hour, int { bool error = ( hour < 0 || hour min < 0 || min > sec < 0 || sec > if (!error) { this. hour = hour; this. min = min; this. sec = sec; } min, int sec) > 24 || 59 ); 34
程式 Using. This (4/4) else { Console. Write. Line( "Time. Set. Time>>參數值不合理"); } } public void Get. Time(out int hour, out int min, out int sec) { hour = this. hour; min = this. min; sec = this. sec; } } 35
練習(2/4) • 函式Swap虛擬碼 Procedure Swap( card 1, card 2 ) 1. temp = card 1; 2. card 1 = card 2; card 1 3. card 2 = temp; temp return card 2 39
練習(3/4) • 宣告並實作以下類別 Card suit : char face. Value : int Set. Card() Get. Card() 40
程式 Overloading. Demo (1/4) using System; namespace Overloading. Demo { /* * 示範多載之應用 * 3/17/2007 */ class Program { static void Main(string[] args) { Adder adder = new Adder(); int i = 0; 42
程式 Overloading. Demo (2/4) "); do { Console. Write( "指定運算數值型態: 0. 結束; 1. 整數; 2. 浮點數: i = int. Parse(Console. Read. Line()); if (i == 0) break; if (i > 2) continue; switch (i) { case 1: Console. Write("輸入第一個整數"); int a = int. Parse(Console. Read. Line()); Console. Write("輸入第二個整數"); int b = int. Parse(Console. Read. Line()); adder. Add. And. Display(a, b); break; 43
程式 Overloading. Demo (3/4) case 2: Console. Write("輸入第一個浮點數"); double ad = double. Parse(Console. Read. Line()); Console. Write("輸入第二個浮點數"); double bd = double. Parse(Console. Read. Line()); adder. Add. And. Display(ad, bd); break; } } while (true); } } /* 模擬加法器 * 3/17/2007 */ 44
程式 Overloading. Demo (4/4) public class Adder { public void Add. And. Display(int a, { int result = a + b; Console. Write. Line("{0} + {1} = result); } public void Add. And. Display(double { double result = a + b; Console. Write. Line("{0} + {1} = result); } } int b) {2}", a, b, a, double b) {2}", a, b, } 45
類別 Table content: int[, ] n. Row : int n. Col : int Content Get. Row. Sum() Get. Col. Sum() Get. Total. Sum() 50
程式 Class. Demo. Program (3/5) * 全部總和 * 27 * */ class Program { static void Main(string[] args) { const int N_ROW = 2; const int N_COL = 3; int[, ] table = new int[N_ROW, N_COL]; table[0, 0] = 3; table[0, 1] = 4; table[0, 2] = 2; table[1, 0] = 5; 53
程式 Class. Demo. Program (4/5) table[1, 1] = 7; table[1, 2] = 6; Table t = new Table(); t. Content = table; int[] row. Sum = t. Get. Row. Sum(); int[] col. Sum = t. Get. Col. Sum(); int total. Sum = t. Get. Total. Sum(); for (int i = 0; i < N_ROW; ++i) { Console. Write. Line("Sum of row " + i + " is " + row. Sum[i]); } 54
程式 Class. Demo. Program (5/5) for (int j = 0; j < N_COL; ++j) { Console. Write. Line("Sum of column " + j + " is " + col. Sum[j]); } Console. Write. Line("Total sum is " + total. Sum); Console. Read. Line(); } } } 55
程式 Class. Demo. Table (1/5) using System; namespace Class. Demo { /* * 表格 * skj 3/31/2008 */ public class Table { private int[, ] content; int n. Row; int n. Col; 56
程式 Class. Demo. Table (2/5) public int[, ] Content { get { return content; } set { content = value; n. Row = content. Get. Upper. Bound(0) n. Col = content. Get. Upper. Bound(1) } } + + 1; 1; 57
程式 Class. Demo. Table (3/5) public int[] Get. Row. Sum() { int[] row. Sum = new int[n. Row]; for (int i = 0; i < n. Row; ++i) { row. Sum[i] = 0; for (int j = 0; j < n. Col; ++j) { row. Sum[i] += content[i, j]; } } return row. Sum; } 58
程式 Class. Demo. Table (4/5) public int[] Get. Col. Sum() { int[] col. Sum = new int[n. Col]; for (int j = 0; j < n. Col; ++j) { col. Sum[j] = 0; for (int i = 0; i < n. Row; ++i) { col. Sum[j] += content[i, j]; } } return col. Sum; } 59
程式 Class. Demo. Table (5/5) public int Get. Total. Sum() { int total. Sum = 0; for (int i = 0; i < n. Row; ++i) { for (int j = 0; j < n. Col; ++j) { total. Sum += content[i, j]; } } return total. Sum; } } } 60
物件 CRC 卡 Object Name: withdraw. Manager Collaborators Responsibilities Ask user for amount to withdraw Verify amount with Account. Manager account. Manager cash. Dispenser Tell cash dispenser to release cash 62
類別 CRC卡 Class Name: Withdraw. Manager Collaborators Responsibilities Ask user for amount to withdraw Account. Manager Verify amount with Account. Manager Cash. Dispenser Tell Cash. Dispenser to release cash 63
UML 類別圖( Class Diagram ) * G. Booch, J. Rumbaugh, and I. Jacobson, The Unified Modeling Language User Guide, Addison Wesley, 1999. 64
構成(Aggregation)之實作 class Company { //…… Department[] department; // created by Company objects Office[] office; // created by Company objects //…… } 65
關連(Association)之實作 class Department { //…… Person manager; // created outside Department Person[] member; // created outside Department //…… } class Person //…… Department //…… } { department; // created outside Person 66
延伸閱讀:系統分析與設計 l l F. P. Brooks, Jr. , The Mythical Man-Month – Essays on Software Engineering, Addison. Wesley, 1975 G. Booch, J. Rumbaugh, and I. Jacobson, The Unified Modeling Language User Guide, Addison Wesley, 1999. I. Jacobson, G. Booch, and J. Rumbaugh, The Unified Software Development Process, Addison Wesley, 1999. R. C. Lee and W. M. Tepfenhart, UML and C++, A Practical Guide to Object-Oriented nd 67
- Slides: 69