Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA 程式設計與資料結構 第三章 物件的設計.

Similar presentations


Presentation on theme: "JAVA 程式設計與資料結構 第三章 物件的設計."— Presentation transcript:

1 JAVA 程式設計與資料結構 第三章 物件的設計

2 食譜 (recipe) 麻婆豆腐的食譜

3 物件(class)的設計

4 new (class之實現) 產生一個新的物件(instance),名為o1 obj1()中有方法m1(),用.來使用該方法
class ObjDemo1{ public static void main(String args[]) { obj1 o1=new obj1(); o1.m1(); }//main }//ObjDemo1 產生一個新的物件(instance),名為o1 obj1()中有方法m1(),用.來使用該方法

5 new (class之實現) 麻婆豆腐 麻記麻婆豆腐 = new 麻婆豆腐(); 麻婆豆腐 陳記麻婆豆腐 = new 麻婆豆腐();
麻婆豆腐 陳記麻婆豆腐 = new 麻婆豆腐(); 錄影機 小錄 = new 錄影機(); 錄影機 小影 = new 錄影機(); 錄影機 小機 = new 錄影機(); 同一個class(菜單)可以new出無限個數的instance(一盤菜),只要給不同的名稱即可

6 Constructor (建構子) Constructor為用來實現class的依據,instance根據constructor來建構
設計一個class時,若是沒有特別設計constructor,那麼內訂為空白的constructor,也就是完全沒有輸入值的constructor。 若是另有設計,則空白的constructor不存在

7 Nested classes 主程式中包含了class A跟B。 Class A中包含了class C。
Class B中包含了class D。

8 包含主程式 之Class 架構

9 Access Control(存取控制) 變數及方法 Public - 可在class之外使用 instanceName.變數名 取得變數
Protected -在繼承的物件以及package之內可以存取此變數 private – 僅能在該class中才能取得變數值

10 Static 修飾變數

11 使用static修飾的方法可以直接使用,無須new一個新的instance。如staticMethod.sum(20)
在字串String及數學Math兩個class中定義了許多被static修飾的方法

12 Constant 固定不變的定數,使用private static final來修飾,例如:
private static final double PI = ;


Download ppt "JAVA 程式設計與資料結構 第三章 物件的設計."

Similar presentations


Ads by Google