instance Ex 511 class C int i class

  • Slides: 136
Download presentation

第五章 物件程式設計 • 什麼是類別、物件、instance ? • Ex 5_1_1 class C { int i; }

第五章 物件程式設計 • 什麼是類別、物件、instance ? • Ex 5_1_1 class C { int i; } class Main { public static void main(String[] args) { C c 1; // 宣告一個物件 c 1 = new C(); // 產生一個物件 } } 10/7/2020 3 精解Java 2程式語言

第五章 物件程式設計 01. class C { 02. int i; 03. } 04. 05. class

第五章 物件程式設計 01. class C { 02. int i; 03. } 04. 05. class Ex 5_2_1 { 06. public static void main(String[] args) { 07. C c 1 = new C(); 08. c 1. i = 10; 09. System. out. println("c 1. i = "+c 1. i); 10. 11. c 1. i = 20; 12. System. out. println("c 1. i = "+c 1. i); 13. } 14. } 10/7/2020 10 輸出結果: c 1. i = 10 c 1. i = 20 精解Java 2程式語言

第五章 物件程式設計 class C { int i; } class Ex 5_2_4 { public static

第五章 物件程式設計 class C { int i; } class Ex 5_2_4 { public static void main(String[] args) { C c 1, c 2; 輸出結果: First => c 1. i = 10, c 2. i = 10 Second => c 1. i = 20, c 2. i = 10 c 1 = c 2 = new C(); // 產生一個新物件,將其位址存到 c 1, c 2。 c 1. i = 10; System. out. println("First => c 1. i = "+c 1. i+", c 2. i = "+c 2. i); c 1 = new C(); // 產生一個新物件,將其位址存到 c 1。 c 1. i = 20; System. out. println("Second => c 1. i = "+c 1. i+", c 2. i = "+c 2. i); } } 10/7/2020 19 精解Java 2程式語言

第五章物件程式設計 – Block 與 Block 的關係 • 區塊之間互不隸屬 (獨立區塊) { // Block A begins

第五章物件程式設計 – Block 與 Block 的關係 • 區塊之間互不隸屬 (獨立區塊) { // Block A begins } // Block A ends { // Block B begins } // Block B ends 10/7/2020 23 精解Java 2程式語言

第五章物件程式設計 • 以宣告位置區分 – Class scope: • 物件變數 (instance variables) • 類別變數 (class variables)

第五章物件程式設計 • 以宣告位置區分 – Class scope: • 物件變數 (instance variables) • 類別變數 (class variables) – Local scope: • 區域變數 (local variables) 10/7/2020 44 精解Java 2程式語言

第五章物件程式設計 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13.

第五章物件程式設計 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 10/7/2020 class N { 執行結果: Before: n 1. i = 10 Before: n 2. i = 10 After: n 1. i = 5 After: n 2. i = 10 int i = 10; // 物件變數 } class Ex 5_4_1 { public static void main(String[] args) { N n 1, n 2; n 1 = new N(); n 2 = new N(); System. out. println("Before: n 1. i = " + n 1. i); System. out. println("Before: n 2. i = " + n 2. i); n 1. i = 5; // 改變 n 1 中的物件變數 i 為 5 System. out. println("After: n 1. i = " + n 1. i); System. out. println("After: n 2. i = " + n 2. i); } } 46 精解Java 2程式語言

第五章物件程式設計 01. class N { 02. static int i = 10; // 定義一個類別變數 03.

第五章物件程式設計 01. class N { 02. static int i = 10; // 定義一個類別變數 03. } 04. 05. class Ex 5_4_4 { 06. public static void main(String[] args) { 07. N n 1, n 2; 08. 09. n 1 = new N(); 10. n 2 = new N(); 11. 12. System. out. println("Before: n 1. i = " + n 1. i); 13. System. out. println("Before: n 2. i = " + n 2. i); 14. 15. n 1. i = 5; // 改變 n 1 中的類別變數 i 為 5 16. 17. System. out. println("After: n 1. i = " + n 1. i); 18. System. out. println("After: n 2. i = " + n 2. i); 19. } 20. } 10/7/2020 49 執行結果: Before: n 1. i = 10 Before: n 2. i = 10 After: n 1. i = 5 After: n 2. i = 5 精解Java 2程式語言

第五章物件程式設計 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13.

第五章物件程式設計 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 10/7/2020 class N { static int i = 10; } 輸出結果: N. i = 5 n. i = 5 class Ex 5_4_5 { static int j; public static void main(String[] args) { j = 6; N. i = 5; // 直接使用類別 N 中的類別變數 i。 N n = new N(); System. out. println("N. i = "+ N. i); // 類別 N 中的 類別變數 i System. out. println("n. i = "+ n. i); // 經由物件 n,讀取類別變數 i。 } } 50 精解Java 2程式語言

第五章物件程式設計 instanceof • 在許多的物件之中我們如何來判定一個 “物件” 是由哪個 “類別” 所產生 的呢? • instanceof 運算子:instanceof 一次只能測試一個物件是否為某 一個類別所產生的物件。

第五章物件程式設計 instanceof • 在許多的物件之中我們如何來判定一個 “物件” 是由哪個 “類別” 所產生 的呢? • instanceof 運算子:instanceof 一次只能測試一個物件是否為某 一個類別所產生的物件。 01. class C {} 02. 執行結果: 03. class Ex 5_4_6 { c is an object of class C! 04. public static void main(String[] args) { 05. C c = new C(); 06. 07. if (c instanceof C) { 08. System. out. println("c is an object of class C!"); 09. else 10. System. out. println("c is not an object of class C"); 11. } 12. } 13. } 10/7/2020 51 精解Java 2程式語言

簡單的鏈結串列 01. class C { 02. int i; 03. C next; //儲存下一個物件的位址 04. }

簡單的鏈結串列 01. class C { 02. int i; 03. C next; //儲存下一個物件的位址 04. } 05. 06. class Ex 5_4_8 { 07. public static void main(String[] args) { 08. C c 1 = new C(); c 1. i = 1; 09. 10. C c 2 = new C(); c 2. i = 2; 11. 12. C c 3 = new C(); c 3. i = 3; 13. 14. c 1. next = c 2; // 將 c 1 與 c 2 連結 15. c 2. next = c 3; // 將c 2 與 c 3 連結 16. 17. for (C p = c 1; p != null; p = p. next) 18. System. out. println(p. i); 19. } 20. } 10/7/2020 58 執行結果: 1 2 3 精解Java 2程式語言

簡單的鏈結串列 01. class C { c 1 32 b 02. int i; 03. C

簡單的鏈結串列 01. class C { c 1 32 b 02. int i; 03. C next; 04. } 05. 32 b 06. class Ex 5_4_9 { 07. public static void main(String[] args) { 37 f 08. C c 1 = new C(); c 1. i = 1; c 1. next 09. 10. c 1. next = new C(); c 1. next. i = 2; 11. c 1. next = new C(); c 1. next. i = 3; 12. 13. for (C p = c 1; p != null; p = p. next) 14. System. out. println(p. i); 15. } 16. } 10/7/2020 59 物件位址 37 f a 12 null c 1. next 執行結果: 1 2 3 精解Java 2程式語言

第五章物件程式設計 01. class Account { 02. private int balance; // 物件變數 03. void clear.

第五章物件程式設計 01. class Account { 02. private int balance; // 物件變數 03. void clear. Account() { balance = 0; } 執行結果: 04. void deposit(int m) { balance += m; } Joe has 300 dollars. 05. // 存錢的方法 06. int get. Balance() { return balance; } wason has 500 dollars. 07. // 顯示目前餘額的方法 08. } 10. class Ex 5_5_1 { 11. public static void main(String[] args) { 12. Account joe = new Account(); // 產生一個帳戶:joe 14. Account wason = new Account(); // 產生一個帳戶:wason 17. joe. clear. Account(); // 將 Joe 的帳戶餘額清空 19. wason. clear. Account(); // 將 Wason 的帳戶餘額清空 22. joe. deposit(300); // Joe 存了 300 元 24. wason. deposit(500); // Wason 存了 500 元 27. // 顯示 Joe 目前的帳戶餘額 28. System. out. println("Joe has : "+ joe. get. Balance() + " dollars"); 31. // 顯示 Wason 目前的帳戶餘額 32. System. out. println("Wason has : "+ wason. get. Balance() + " dollars"); 34. } 35. } 10/7/2020 61 精解Java 2程式語言

第五章物件程式設計 • 資料的接收與傳遞 – 要傳遞的資料其型態及個數必須與所要呼叫的方法所要接收的資料的資料 型態與個數完全一致。 01. class C { 02. void do. Something(int

第五章物件程式設計 • 資料的接收與傳遞 – 要傳遞的資料其型態及個數必須與所要呼叫的方法所要接收的資料的資料 型態與個數完全一致。 01. class C { 02. void do. Something(int i, double d, String s) {} 03. } 04. 05. class Ex 5_5_2 { 06. public static void main(String[] args) { 07. C c = new C(); 08. 09. c. do. Something(10, 3. 0); 10. // 編譯錯誤! 少了一個 String 11. c. do. Something(10, 3, "hello"); 12. // 編譯錯誤! 第二個參數應該是 double 13. c. do. Something(10. 0, 3. 0, "hello”); 14. // 編譯錯誤! 第一個參數應該是 int 16. c. do. Something(10, 3. 0, "hello"); // 正確! 17. } 18. } 10/7/2020 66 精解Java 2程式語言

第五章物件程式設計 01. 02. 03. 04. 05. 06. 07. 08. 09. 10 11. 12. 13.

第五章物件程式設計 01. 02. 03. 04. 05. 06. 07. 08. 09. 10 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 10/7/2020 class C { void do. Task. One(int i, String s) {} void do. Task. Two(int[] i, String[] s, String t) {} void do. Task. Three(int[] i, int j, String s) {} } class Ex 5_5_3 { public static void main(String[] args) { C c = new C(); int i[] = new int[3]; String[] s = {"hello", "world"}; c. do. Task. One(i, s); // 錯誤! i的型態為int[],s 的型態為String[]。 c. do. Task. One(i[1], s[1]); // 錯誤! i[1]的型態為int,s[1]的型態為String。 c. do. Task. Two(i, s, s); // 錯誤! c. do. Task. Two(i, s, s[0]); // 錯誤! } } c. do. Task. Three(i[0], i[1], s[0]); // 錯誤! c. do. Task. Three(i, i[1], s[0]); // 錯誤! 67 精解Java 2程式語言

第五章物件程式設計 01. 02. 03. 04. 06. 09. 10. 11. 12. 13. 14. 15. 17.

第五章物件程式設計 01. 02. 03. 04. 06. 09. 10. 11. 12. 13. 14. 15. 17. 18. 19. 20. 22. 23. 24. 26. 27. 28. 29. 10/7/2020 class Number { private int n; // 物件變數 void set. Number(int i) { n = i; } // 設定變數 n 的值。 int get. Number() { return n; } // 傳回變數 n 的值。 boolean is. Even() { if (n % 2 == 0) return true; else return false; } 執行結果: 10 is even: true } class Ex 5_5_5 { public static void main(String[] args) { Number n = new Number(); n. set. Number(10); // 將物件 n 是否為偶數的結果存到 boolean 變數 b 中。 boolean b = n. is. Even(); System. out. println(n. get. Number() + " is even: " + b); // 直接將物件 n 是否為偶數的結果與 String 結合後輸出。 System. out. println(n. get. Number() + " is even: " + n. is. Even()); } } 72 精解Java 2程式語言

第五章物件程式設計 精簡化程式: 01. class Ex 5_5_6 { 執行結果: 02. public static void main(String[] args)

第五章物件程式設計 精簡化程式: 01. class Ex 5_5_6 { 執行結果: 02. public static void main(String[] args) { n is odd 03. Number n = new Number(); 04. n. set. Number(5); 05. 06. if (n. is. Even()) 07. System. out. println("n is even"); 08. else 09. System. out. println("n is odd"); 10. } 11. } 10/7/2020 73 精解Java 2程式語言

第五章物件程式設計 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13.

第五章物件程式設計 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 15. 16. 17. 18. 10/7/2020 class C { private int i; // 物件變數 private static int j = 5; // 類別變數 輸出結果: C's class variable j is 5 c's instance variable i is 10 public static int get. J() { return j; } // 類別方法 public int get. I() { return i; } // 物件方法 public void set. I(int i) { this. i = i; } // 物件方法 } class Ex 5_5_8 { public static void main(String[] args) { System. out. println("C' class variable j is: " + C. get. J()); C c = new C(); c. set. I(10); System. out. println("c's instance variable i is: "+c. get. I()); } } 79 精解Java 2程式語言

第五章物件程式設計 01. class C { 02. int i; 03. 04. C() { // 定義預設建構子

第五章物件程式設計 01. class C { 02. int i; 03. 04. C() { // 定義預設建構子 05. System. out. println("I am Constructor!"); 06. i = 10; 07. } 執行結果: 08. I am Constructor! 09. int get. Info() { return i; } 10 11. 12. class Ex 5_5_10 { 13. public static void main(String[] args) { 14. C c = new C(); 15. System. out. println(c. get. Info()); 16. } 17. } 10/7/2020 84 精解Java 2程式語言

第五章物件程式設計 01. class Number { 02. private int n; // 物件變數 03. 04. Number(int

第五章物件程式設計 01. class Number { 02. private int n; // 物件變數 03. 04. Number(int m) { m = n; } // 建構子 05. 06. void set. Number(int i) { n = i; } // 設定變數 n 07. int get. Number() { return n; } // 傳回變數 n 08. 09. boolean is. Even() { 10. if (n % 2 == 0) 11. return true; 12. else 13. return false; 14. } 15. } 17. class Ex 5_5_11 { 18. public static void main(String[] args) { 19. Number n = new Number(10); 20. n. set. Number(10); 21. // 在建構子中直接設定其值 22. 23. // 將物件 n 是否為偶數的結果存到 boolean 變數 b 中。 24. boolean b = n. is. Even(); 25. System. out. println(n. get. Number() + " is even: " + b); 26. 27. // 直接將物件 n 是否為偶數的結果與 String 結合後輸出。 28. System. out. println(n. get. Number() + “ is even: " + n. is. Even()); 29. } 30. } 執行結果: 10 is even: true 10/7/2020 85 精解Java 2程式語言

第五章物件程式設計 • 方法重載:我們可以將上述的方法定義,全 部定義為同一個方法名稱: – int add(int i, int j) { return i +

第五章物件程式設計 • 方法重載:我們可以將上述的方法定義,全 部定義為同一個方法名稱: – int add(int i, int j) { return i + j; } – float add(float i, float j) { return i + j; } – double add(double i, double j) { return i + j; } – int add(int i, float f) { return i + (int) f; } 10/7/2020 88 精解Java 2程式語言

第五章物件程式設計重載建構子 • 使用 Overloaded Constructors (重載建構子) 01. class Student { 20. class Ex 5_5_12

第五章物件程式設計重載建構子 • 使用 Overloaded Constructors (重載建構子) 01. class Student { 20. class Ex 5_5_12 { 02. static int count = 0; // 類別變數,紀錄學生總數。 21. public static void main(String[] args) { 03. int id; 22. Student john = new Student("john"); 04. String name; 23. Student mary = new Student(1001, "mary"); 05. 24. 06. Student(String n) { 25. System. out. println("Student name: "+john. get. Name()+", 07. id = ++count; //如果不提供學號,以流水號代替。 26. id = "+john. get. ID()); 08. name = n; 27. System. out. println("Student name: "+mary. get. Name()+", 09. } 28. id = "+mary. get. ID()); 10. 29. } 11. Student(int i, String n) { 30. } 12. id = i; 13. name = n; count++; 14. } 15. 16. String get. Name() { return name; } 執行結果: 17. int get. ID() { return id; } Student name: john, id = 1 18. } Student name: mary, id = 1001 10/7/2020 92 精解Java 2程式語言

第五章物件程式設計 資訊封裝 • public 方法通常都被視為是物件與外界溝通的介面。因為從物件的使用 者來說,他們只被允許使用類別中所定義的 public 方法。 01. class C { 02. private

第五章物件程式設計 資訊封裝 • public 方法通常都被視為是物件與外界溝通的介面。因為從物件的使用 者來說,他們只被允許使用類別中所定義的 public 方法。 01. class C { 02. private int i = 5; // 私有資源,禁止外部存取! 03. public void set. I(int j) { i = j; } // public 方法 04. public int get. I() { return i; } // public 方法 05. } 06. 07. class Ex 5_6_3 { 08. public static void main(String[] args) { 09. C c = new C(); 10. System. out. println("c. i = "+c. get. I()); 11. 12. c. set. I(10); 13. System. out. println("*c. i = "+c. get. I()); 14. } 15. } 10/7/2020 100 執行結果: c. i = 5 *c. i = 10 精解Java 2程式語言

第五章物件程式設計 資訊封裝 • 所謂的 private 方法就是定義物件內部的 作方式。這些 作方式,並 不需要外界知曉。因此,物件的使用者也無法使用這些 private 方法。 01. class C

第五章物件程式設計 資訊封裝 • 所謂的 private 方法就是定義物件內部的 作方式。這些 作方式,並 不需要外界知曉。因此,物件的使用者也無法使用這些 private 方法。 01. class C { 02. private int i; 03. private boolean even; 04. 05. public C(int j) { 06. even = check. Even(j); 07. i = j; 08. } 09. 10. private boolean check. Even(int j) { 11. return i % 2 == 0 ? true : false; 12. } 13. 14. public boolean is. Even() { return even; } 15. } 16. 17. class Ex 5_6_4 { 18. public static void main(String[] args) { 19. C c = new C(10); 20. System. out. println("c is even: "+c. is. Even()); 21. } 22. } 10/7/2020 101 執行結果: c is even: true 精解Java 2程式語言

第五章物件程式設計 資訊封裝 01. class C { 02. private int i; 03. // private boolean

第五章物件程式設計 資訊封裝 01. class C { 02. private int i; 03. // private boolean even; 04. 05. public C(int j) { 06. // even = check. Even(j); 07. i = j; 08. } 09. 10. private boolean check. Even(int j) { 11. return i % 2 == 0 ? true : false; 12. } 13. 14. public boolean is. Even() { return check. Even(i); } 15. } 10/7/2020 103 精解Java 2程式語言

第五章物件程式設計 01. class. C { 02. private int i; 03. public C (int i)

第五章物件程式設計 01. class. C { 02. private int i; 03. public C (int i) { 04. this. i = i; 05. } 06. } 07. 08. class Ex 5_6_6 { 09. public static void main(String[] args) { 10. C c = new C(10); 11. } 12. } 10/7/2020 105 精解Java 2程式語言

第五章物件程式設計 • this 也可以用來呼叫同一個類別中其他的建構子。 01. class C { 02. private int i; 03. private

第五章物件程式設計 • this 也可以用來呼叫同一個類別中其他的建構子。 01. class C { 02. private int i; 03. private String s; 04. 05. public C() { this(0, ""); } // 第 1 個建構子 06. public C(int i) { this(i, ""); } // 第 2 個建構子 07. public C(int i, String s) { // 第 3 個建構子 08. this. i = i; 09. this. s = s; 10. } 11. } 10/7/2020 106 精解Java 2程式語言

第五章物件程式設計 this keyword 1/3 • this 還有一個功能,就是在類別中代表自己。 01. 02. 03. 04. 05. 06. 07.

第五章物件程式設計 this keyword 1/3 • this 還有一個功能,就是在類別中代表自己。 01. 02. 03. 04. 05. 06. 07. 08. 09. 10. 11. 12. 13. 14. 10/7/2020 class Bag { private C[] c. List; private int count; public Bag() { c. List = new C[5]; } public void add(C c) { c. List[count++] = c; } public void print. Out. List() { for (int i=0; i<count; i++) System. out. println(c. List[i]. get. Number()); } } 108 精解Java 2程式語言

第五章物件程式設計 this keyword 2/3 16. class C { 17. private static Bag bag =

第五章物件程式設計 this keyword 2/3 16. class C { 17. private static Bag bag = new Bag(); // 類別物件 18. private int i; // 物件變數 19. 20. public C() { add. To. List(); } 21. 22. public C(int i) { 23. this. i = i; 24. add. To. List(); 25. } 26. 27. private void add. To. List() { 28. bag. add(this); // 將自己加入物件 bag 中! 29. } 30. 31. public int get. Number() { return i; } 32. 33. public static void print. List() { 34. bag. print. Out. List(); 35. } 36. } 10/7/2020 109 精解Java 2程式語言

第五章物件程式設計 this keyword 3/3 38. class Ex 5_6_9 { 39. public static void main(String[]

第五章物件程式設計 this keyword 3/3 38. class Ex 5_6_9 { 39. public static void main(String[] args) { 40. for (int i=0; i<3; i++) { 41. new C(i); // 產生 3 個物件並自動加入 List 中 42. } 43. 44. C. print. List(); // 將 List 印出 45. } 46. } 執行結果: 0 1 2 10/7/2020 110 精解Java 2程式語言

第五章物件程式設計 01. class C { 02. int i; 03. } 04. 05. class Ex

第五章物件程式設計 01. class C { 02. int i; 03. } 04. 05. class Ex 5_7_1 { 06. public static void main(String[] args) { 07 int[] int. Arr; // 原始型態 int 陣列 08. C[] c. Arr; // 參考型態 C 陣列 09. 10. int. Arr = new int[3]; 11. c. Arr = new C[3]; 12. 13. int. Arr[0] = 10; 14. c. Arr[0] = new C(); // 產生物件 15. c. Arr[0]. i = 20; // 指定物件中的 i 值為 20 16. 17. System. out. println(" int. Arr[0] = "+int. Arr[0]+ " , 18. c. Arr[0]. i="+c. Arr[0]. i); 19. 20. C c 1 = new C(); c 1. i = 30; 21. c. Arr[1] = c 1; // 將 c 1 的位址 copy 至 c. Arr[1] 22. 23. System. out. println("c 1 = "+c 1. i+ ", 24. c. Arr[1] = "+c. Arr[1]. i); 25. } 26. } 10/7/2020 112 執行結果: int. Arr[0] = 10, c. Arr[0]. i=20 c 1 = 30, c. Arr[1] = 30 精解Java 2程式語言

第五章物件程式設計物件陣列的初始化 • 參考型態 [] 陣列名稱 = {new敘述列} 敘述列 01. class C { 02. private

第五章物件程式設計物件陣列的初始化 • 參考型態 [] 陣列名稱 = {new敘述列} 敘述列 01. class C { 02. private int i; 執行結果: 03. C() { i = 1; } c. Arr[0]=1 04. C(int i) { this. i = i; } c. Arr[1]=1 05. 06. public int get. I() { return i; } c. Arr[2]=10 07. } 08. 09. class Ex 5_7_2 { 10. public static void main(String[] args) { 11. int i. Arr[] = {1, 2, 3}; 12. C[] c. Arr = {new C(), new C(10)}; 13. 14. for(int i=0; i<c. Arr. length; i++) 15. System. out. println("c. Arr["+i+"]="+c. Arr[i]. get. I()); 16. } 17. } 10/7/2020 113 精解Java 2程式語言

第五章物件程式設計物件陣列的使用範例 01. class Menu. Item { 02. private String menu. Caption; // menu 主標題

第五章物件程式設計物件陣列的使用範例 01. class Menu. Item { 02. private String menu. Caption; // menu 主標題 03. private String[] sub. Menu; // 子 menu 之標題 執行結果: 04. 05. public Menu. Item(String m, String[] s) { Menu. Item: A 06. menu. Caption = m; Sub. Menu=> 07. sub. Menu = s; a 1 08. } a 2 09. Menu. Item: B 10. public void print. Menu() { // 將 menu 及其子 menu 印出 Sub. Menu=> 11. System. out. println("Menu. Item: "+menu. Caption); 12. System. out. println("t. Sub. Menu=>"); b 1 13. b 2 14. for(int i=0; i<sub. Menu. length; i++) 15. System. out. println("tt"+sub. Menu[i]); 16. } 17. } 19. class Ex 5_7_3 { 20. public static void main(String[] args) { 21. Menu. Item[] menu = { new Menu. Item("A", new String[] {"a 1", "a 2"} ), 23. new Menu. Item("B", new String[] {"b 1", "b 2"}) 24. }; 26. for (int i=0; i<menu. length; i++) 27. menu[i]. print. Menu(); 28. } 29. } 10/7/2020 115 精解Java 2程式語言

第五章物件程式設計物件陣列的使用範例 • 日期陣列 01. class Date { 02. private int year; 執行結果: 03. private

第五章物件程式設計物件陣列的使用範例 • 日期陣列 01. class Date { 02. private int year; 執行結果: 03. private int month; Year: 1986, Month: 1, Day: 10 04. private int day; Year: 1985, Month: 10, Day: 23 05. 06. public Date(int y, int m, int d) { 07. year = y; month = m; day = d; 08. } 09. 10. public int get. Year() { return year; } 11. public int get. Month() { return month; } 12. public int get. Day() { return day; } 13. } 14. 15. class Ex 5_7_4 { 16. public static void main(String[] args) { 17. Date[] d = {new Date(1986, 1, 10), 18. new Date(1985, 10, 23)}; 19. for (int i=0; i<d. length; i++) 20. System. out. println("Year: "+d[i]. get. Year()+ 21. ", Month: "+d[i]. get. Month()+", Day: " 22. +d[i]. get. Day()); 23. } 24. } 10/7/2020 116 精解Java 2程式語言

第五章物件程式設計 21. 22. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34.

第五章物件程式設計 21. 22. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. } 10/7/2020 public Ex 5_8_1() { // System. out. println(n); // 錯誤!無法由外部存取成員類別中的資源 memb = new Member(); System. out. println(memb. n + ", "+memb. get. N()); } public int get. M() { return m; } public static void main(String[] args) { Ex 5_8_1 o = new Ex 5_8_1(); System. out. println(o. get. M()); } 122 執行結果: 3, 3 9 精解Java 2程式語言

第五章物件程式設計 01. class Ex 5_8_2 { 03. int i=5; 04. Member m; 05. 06.

第五章物件程式設計 01. class Ex 5_8_2 { 03. int i=5; 04. Member m; 05. 06. class Member { 07. int i=10; 08. Member() { 09. Ex 5_8_2. this. i=this. i; // this. i 是指 Member. this. i 10. } 11. } 12. 13. public Ex 5_8_2() { 14. Member m = new Member(); 15. } 17. public String to. String() { 18. return “ “+i; 19. } 21. public void set. I(int i) { 22. this. i = i; // this. i是指Ex 5_8_2. this. i 23. } 25. public static void main(String[] args) { 26. Ex 5_8_2 t = new Ex 5_8_2(); 27. System. out. println(t. to. String()); 28. t. set. I(8); 29. System. out. println(t. to. String()); 30. } 31. } 10/7/2020 123 輸出結果: 10 8 精解Java 2程式語言

第五章物件程式設計 01. class Main { 02. private int i; 03. public int k; 04.

第五章物件程式設計 01. class Main { 02. private int i; 03. public int k; 04. Member. Class mc = new Member. Class(); 05. 06. class Member. Class { 07. private int j; 09. Member. Class() { 10. // 使用擴充語法存取所屬物件的變數 k! 11. Main. this. i = 3; 12. Main. this. k = 7; 14. // 直接存取所屬物件的變數 k! 15. i = 10; j = 5; k = 3; 16. } 18. private int get. J 1(){ return j; } 19. public int get. J 2() { return j; } 20. } 22. Main() { 23. mc. j = 20; 24. // 根據類別生存範圍的存取規則,private變數j可直接存取! 25. i = mc. get. J 1(); 26. } 27. 28. public Member. Class handle() { return new Member. Class(); } 29. public int get. I() { return i; } 30. } 10/7/2020 124 32. class Ex 5_8_3 { 33. public static void main(String[] args) { 34. Main oc = new Main(); 35. Main. Member. Class mc = oc. handle(); 36. Main. Member. Class mc 1 = oc. new Member. Class(); 37. 38. System. out. println(oc. get. I()); 39. System. out. println(mc 1. get. J 2()); 40. } 41. } 執行結果: 10 5 精解Java 2程式語言

第五章物件程式設計 • public / private成員類別 – 宣告為 public 的成員類別就可以為他人所用;而宣告為 private 的成 員類別的存取範圍就僅限於所屬類別之中,無法為外界使用。 01. class

第五章物件程式設計 • public / private成員類別 – 宣告為 public 的成員類別就可以為他人所用;而宣告為 private 的成 員類別的存取範圍就僅限於所屬類別之中,無法為外界使用。 01. class C { 02. public class D { 03. private int i; 04. public D() { 05. i = 10; 06. } 07. public int get. Info() { 08. return i; 09. } 10. } 11. private class E { 12. private int i; 13. public E() { 14. i = 5; 15. } 16. } 17. } 10/7/2020 19. class Ex 5_8_4 { 20. public static void main(String[] args) { 21. C c = new C(); 22. C. D d = c. new D(); 23. // C. E e = c. new E(); // 編譯錯誤! 24. System. out. println(d. get. Info()); 25. } 26. } 執行結果: 10 125 精解Java 2程式語言

第五章物件程式設計 01. class Ex 5_8_5 { 02. private int i = 10; 04. public

第五章物件程式設計 01. class Ex 5_8_5 { 02. private int i = 10; 04. public Ex 5_8_5() { // 建構子 06. class Local { // 區域類別 07. int i = 5; 09. protected void g() { 10. Ex 5_8_5. this. i = 3; 11. //存取所屬類別的 private int i 12. } 13. public String to. String() { 14. return Integer. to. String(i); 15. } 16. } 17. Local l = new Local(); 18. l. g(); 20. System. out. println("Local: "+l. to. String()); 21. } 23. public String to. String() { 24. return Integer. to. String(i); 25. } 27. public static void main(String[] args) { 28. Ex 5_8_5 m = new Ex 5_8_5(); 29. System. out. println("Ex 5_8_5: "+m. to. String()); 30. } 10/7/2020 31. } 127 執行結果: Local: 5 Ex 5_8_5: 3 精解Java 2程式語言

第五章物件程式設計 01. interface Interf { 02. public void test(); 03. } 05. class Ex

第五章物件程式設計 01. interface Interf { 02. public void test(); 03. } 05. class Ex 5_8_6 { 06. int j = 0; 07. public Interf f(final int i) { 08. class Local implements Interf { 09. Local() { 10. test(); 11. } 12. public void test() { 13. j = i; 14. } 15. public String to. String() { 16. return Integer. to. String(j); 17. } 18. } 19. return (new Local()); 20. // 產生並傳回一個 Local 類別的物件 21. } 23. public static void main(String[] args) { 24. Ex 5_8_6 m = new Ex 5_8_6(); 25. Interf inf = m. f(5); 26. // 物件 inf 指向 m. f(5) 傳回之物件 27. System. out. println("Inf: "+inf. to. String()); 28. } 29. } 10/7/2020 129 執行結果: Inf: 5 精解Java 2程式語言

第五章物件程式設計範例:堆疊 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44.

第五章物件程式設計範例:堆疊 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. } 01. public class Bag { 02. private Stack s; 04. private class Stack { // 成員類別 05. int[] ia; 06. int index = -1; 07. final int max. Slots = 5; 09. Stack() { 10. ia = new int[max. Slots]; 11. } 12. public void push(int i) { 13. if (index + 1 < max. Slots) 14. ia[++index] = i; 15. else 16. System. out. println("Error! Stack Overflow"); 17. } 18. public int pop() { 19. if (index - 1 >= 0) 20. return ia[index--]; 21. else 22. System. out. println("Error! Stack Underflow"); 23. return -1; 24. } 26. public void list. Elem() { 27. for(int i = 0; i < ia. length; i++) 28. System. out. println("["+i+"]=>"+ia[i]); 29. } 30. } 10/7/2020 130 public Bag() { s = new Stack(); } public void add(int i) { s. push(i); } public int get() { return s. pop(); } public void list. All() { s. list. Elem(); } public static void main(String[] args) { Bag b = new Bag(); b. add(5); b. add(23); System. out. println("The content of the Bag: "); b. list. All(); } 執行結果: The content of the Bag: [0]=>5 [1]=>23 [2]=>0 [3]=>0 [4]=>0 精解Java 2程式語言

第五章物件程式設計5. 9綜合範例 • 範例一:簡單的加法程式 執行結果: 3+5=8 3 + 4 + 5 = 12 3

第五章物件程式設計5. 9綜合範例 • 範例一:簡單的加法程式 執行結果: 3+5=8 3 + 4 + 5 = 12 3 + 4 + 5 + 6 = 18 01. class Adder { 02. public int do. Add(int i, int j) { 03. return i+j; 04. } 05. } 06. 07. class A { 08. public static void main(String[] args) { 09. Adder adder = new Adder(); 10. System. out. println(“ 3 + 5 = “+ adder. do. Add(3, 5)); 11. System. out. println(“ 3 + 4 + 5 = “+ adder. do. Add(3, adder. do. Add(4, 5))); 12. System. out. println(“ 3 + 4 + 5 + 6 = “+ 13. adder. do. Add(3, 4), adder. do. Add(5, 6))); 14. } 15. } 10/7/2020 131 精解Java 2程式語言

第五章物件程式設計 01. import java. io. *; 02. 執行結果: 03. class Adder 2 { 04.

第五章物件程式設計 01. import java. io. *; 02. 執行結果: 03. class Adder 2 { 04. public static void main(String[] args) { Enter Int: 30 05. Adder adder = new Adder(); Enter Int: 39 06. int i = 0, j = 0; 30 + 39 = 69 07. 08. try { 09. Buffered. Reader in = new Buffered. Reader( 10. new Input. Stream. Reader(System. in)); 11. 12. System. out. print("Enter Int: "); i = Integer. parse. Int(in. read. Line()); 13. System. out. print("Enter Int: "); j = Integer. parse. Int(in. read. Line()); 14. 15. } catch(Exception e) {} 16. 17. System. out. println(i + "+ j + " = " + adder. do. Add(i, j)); 18. } 19. } 10/7/2020 132 精解Java 2程式語言

第五章物件程式設計 • 範例二:裝整數亂數的袋子 01. public class Random. Bag { 02. private int[] bag; 03.

第五章物件程式設計 • 範例二:裝整數亂數的袋子 01. public class Random. Bag { 02. private int[] bag; 03. 04. public Random. Bag() { 05. bag = new int[100]; 06. for (int i=0; i<100; i++) 07. bag[i] = (int) (Math. random()*1000); 08. } 09. public int get. Random() { 10. return bag[(int)(Math. random()*100)]; 11. } 12. } 13. 14. class R { 15. public static void main(String[] args) { 16. Random. Bag r = new Random. Bag(); 17. for (int i=0; i<10; i++) 18. System. out. println(r. get. Random()); 19. } 20. } 10/7/2020 133 執行結果: 382 930 964 892 946 324 634 280 272 588 精解Java 2程式語言

執行結果: Enter your guess: 34 You lose! The answer is: 97 • 範例三:猜數字遊戲 01.

執行結果: Enter your guess: 34 You lose! The answer is: 97 • 範例三:猜數字遊戲 01. import java. io. *; 02. 03. public class Guess { 04. private int answer; 05. 06. public Guess() { 07. answer = (int) (Math. random() * 100); 08. } 09. 10. public boolean attempt(int g) { 11. if (answer == g) 12. return true; 13. else 14. return false; 15. } 16. 17. public int get. Answer() { return answer; } 18. } 10/7/2020 20. class G { 21. public static void main(String[] args) { 22. Guess g = new Guess(); 23. int guess = 0; 25. try { 26. Buffered. Reader in = new Buffered. Reader( 27. new Input. Stream. Reader(System. in)); 28. 29. System. out. print("Enter your guess: "); 30. boolean b = g. attempt(Integer. parse. Int(in. read. Line())); 31. 32. if (b) 33. System. out. println("You got me!"); 34. else 35. System. out. println("n. You lose!n. The answer is: " 36. + g. get. Answer()); 38. } catch(Exception e) {} 39. } 40. } 134 精解Java 2程式語言

01. import java. io. *; 02. 03. public class Guess 2 { 04. private

01. import java. io. *; 02. 03. public class Guess 2 { 04. private int answer; 05. 06. public Guess 2() { 07. answer = (int) (Math. random() * 100); 08. } 09. 10. public int get. Answer() { return answer; } 11. 12. public int get. Comp. Result(int g) { 13. if (g > answer) 14. return 1; 15. else if (g < answer) 16. return -1; 17. else 18. return 0; 19. } 20. } 10/7/2020 22. class G { 23. public static void main(String[] args) { 24. Guess 2 g = new Guess 2(); 25. boolean done = false; 26. int reply = 0; // 使用者輸入的答案 27. int result = 0; // 答案比對結果 29. try { 30. Buffered. Reader in = new Buffered. Reader( 31. new Input. Stream. Reader(System. in)); 33. while (!done) { 34. System. out. print("Enter your guess: 35. (0< guess <100, -1 to exit)"); 36. reply = Integer. parse. Int(in. read. Line()); 37. done = reply < 0 ? true : false; 38. result = g. get. Comp. Result(reply); 40. switch(result) { 41. case 0: 42. System. out. println("You got me!"); 43. break; 44. case 1: 45. System. out. println("Try Again! 46. The answer is smaller! n"); 47. break; 48. case -1: 49. System. out. println("Try Again! 50. The answer is bigger: n"); 51. break; 52. } 53. } 54. } catch(Exception e) {} 55. } 56. } 135 精解Java 2程式語言

第五章物件程式設計 執行結果: Enter your guess: (0 < guess < 100, -1 to exit) 45

第五章物件程式設計 執行結果: Enter your guess: (0 < guess < 100, -1 to exit) 45 Try Again! The answer is bigger: Enter your guess: (0 < guess < 100, -1 to exit) 70 Try Again! The answer is bigger: Enter your guess: (0 < guess < 100, -1 to exit) 80 Try Again! The answer is bigger: Enter your guess: (0 < guess < 100, -1 to exit) 97 Try Again! The answer is smaller! Enter your guess: (0 < guess < 100, -1 to exit) 95 You got me! 10/7/2020 136 精解Java 2程式語言