Java class Account private int balance void clear












物件與類別之Java程式實例 class Account { private int balance; //宣告一帳戶餘額 void clear. Account() { balance = 0; } void deposit(int m) { balance = balance + m; } int get. Balance() { return balance; } //清空帳戶餘額的 "方法" // 存錢的 "方法" // 顯示目前餘額的 "方法" } class E { public static void main(String[] args) { Account joe = new Account(); Account wason = new Account(); joe. clear. Account(); wason. clear. Account(); joe. deposit(300); wason. deposit(500); // 產生一個帳戶:joe // 產生一個帳戶:wason // 將 Joe 的帳戶餘額清空 // 將 Wason 的帳戶餘額清空 // Joe 存了 300 元 // Wason 存了 500 元 輸出結果: Joe has 300 dollars. // 顯示 Joe, Wason 目前的帳戶餘額 Wason has 500 dollars. System. out. println("Joe has " + joe. get. Balance() + "dollars. "); System. out. println("Wason has " + wason. get. Balance() + "dollars. "); } } 系統分析與設計 楊子青 12



封裝資料之Java程式實例 class C { private int i; public int j; int k; } // 私有資料 // 公開資料 // 預設存取資料 class D { public static void main(String[] args) { C c = new C(); c. i = 5; // Error! c. i 是 private資料,禁止存取! c. j = 10; // OK! c. k = 15; // OK! } } 系統分析與設計 楊子青 15

封裝方法之Java程式實例 class Account { private int balance; // 私有資料 void public clear. Account() { balance = 0; } void deposit(int m) { balance = balance + m; } private int get. Balance() { return balance; } // 公開方法 // 預設存取方法 // 私有方法 } class D { public static void main(String[] args) { Account joe = new Account(); joe. clear. Account(); joe. deposit(300); // OK! System. out. println(“Joe has ” + joe. get. Balance() + “dollars. ”); // ERROR! 使用私有方法 } } 系統分析與設計 楊子青 16



繼承之Java程式實例 class C { private int i; public void set. Info(int x) { i = x; } public int get. Info() { return i; } } class D extends C {} // 類別 D 繼承類別 C class E extends C {} // 類別 E 繼承類別 C class F { public static void main(String[] args) { D d = new D(); E e = new E(); d. set. Info(5); e. set. Info(7); 執行結果: The value of d is 5 The value of e is 7 System. out. println("The value of d is "+d. get. Info()); System. out. println("The value of e is "+e. get. Info()); } } 系統分析與設計 楊子青 19


變數覆寫之Java程式實例 class C { int i = 10; } class D extends C { int i = 5; } class E { public static void main(String[] args) { D d = new D(); System. out. println("d = "+d. i); } } 執行結果: d=5 系統分析與設計 楊子青 21

方法覆寫之Java程式實例 class C { void f(String s) { System. out. println("f in class C: "+s); } } class D extends C { void f(String s) { System. out. println("f in class D: "+s); } } class E { public static void main(String[] args) { D d = new D(); d. f("hello"); } } // 父類別的方法被覆寫! 執行結果: f in class D: hello 系統分析與設計 楊子青 22



同名異式之Java程式實例 abstract class Shape { public abstract void f(); } class E { public static void main (String[] args) { Shape[] s = new Shape[] { new Triangle(), new Rectangle(), new Circle() }; class Triangle extends Shape { public void f() { System. out. println("Triangle!"); } } for (int i=0; i<s. length; i++) { s[i]. f(); } } } class Rectangle extends Shape { public void f() { System. out. println("Rectangle!"); } } class Circle extends Shape { public void f() { System. out. println("Circle!"); } } 執行結果: Triangle! Rectangle! Circle! 系統分析與設計 楊子青 25

1. 6超荷(或稱過載,Overload) n 在同一類別中有相同名稱的操作,依參數個數 及參數資料型態來判斷要使用哪一個操作,又 稱靜態多型。 – 例如: class C { int add(int i, int j) { return i + j; } int add(int i, int j, int k) { return i + j + k; } String add(String i, String j) { return i + j; } } 執行結果: 3 + 10 = 13 1+2+5=8 'abc' + 'xyz' = abcxyz class D { public static void main(String[] args) { C c 1 = new C(); System. out. println(“ 3 + 10 = "+c 1. add(3, 10)); System. out. println("1 + 2 + 5 = "+c 1. add(1, 2, 5)); System. out. println("'abc' + 'xyz' = "+c 1. add("abc", "xyz")); } } 系統分析與設計 楊子青 26
















Software Ideas Modeler n 官方網站: http: //www. softwareideas. net/ 下載:http: //www. softwareideas. net/en/download n 安裝: UMLVideo 02: Install and Run Software Ideas Modeler n 系統分析與設計 楊子青 42
- Slides: 42