Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)

Slides:



Advertisements
Similar presentations
1 生物計算期末作業 暨南大學資訊工程系 2003/05/13. 2 compare f1 f2  只比較兩個檔案 f1 與 f2 ,比完後將結果輸出。 compare directory  以兩兩比對的方式,比對一個目錄下所有檔案的相 似程度。  將相似度很高的檔案做成報表輸出,報表中至少要.
Advertisements

Divide-and-Conquer. 什麼是 divide-and-conquer ? Divide 就是把問題分割 Conquer 則是把答案結合起來.
6-1 指標簡介 6-2 指標與陣列 6-3 動態配置記憶體 6-4 本章綜合練習
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
Introduction to Java Programming Lecture 13 Classes I OO Programming.
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
: A-Sequence 星級 : ★★☆☆☆ 題組: Online-judge.uva.es PROBLEM SET Volume CIX 題號: Problem D : A-Sequence 解題者:薛祖淵 解題日期: 2006 年 2 月 21 日 題意:一開始先輸入一個.
:Word Morphing ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10508:word morphing 解題者:楊家豪 解題日期: 2006 年 5 月 21 日 題意: 第一行給你兩個正整數, 第一個代表下面會出現幾個字串,
PowerPoint2010 李燕秋 版面配置 版面配置指的是每一個頁面的內容配置 方式,不同的版面配置會有對應的母片。
Reference, primitive, call by XXX 必也正名乎 誌謝 : 部份文字取於前輩 TAHO 的文章.
What is static?. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1; this.instanceX =
自由進入及離開. 定義  長期 ─ 是指生產者能夠改變所有生產因素的情況。  自由進入及離開 ─ 是指公司能夠自由進入及離開市場而不受限 制。
: Factstone Benchmark ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: : Factstone Benchmark 解題者:鐘緯駿 解題日期: 2006 年 06 月 06 日 題意: 假設 1960.
1 Advanced Chemical Engineering Thermodynamics Appendix BK The Generalized van der Waals Partition Function.
建立靜態資料成員 關鍵字: static 靜態資料成員不屬於特定物件,而是該類物件 所共同擁有的,可做為類別範圍裡的靜態變數。
:New Land ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11871: New Land 解題者:施博修 解題日期: 2011 年 6 月 8 日 題意:國王有一個懶兒子,為了勞動兒子,他想了一個 辦法,令他在某天早上開始走路,直到太陽下山前,靠.
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
JAVA 程式設計與資料結構 第二章 JAVA 程式基本概念及操作. 第一個程式 /* * 這是第一個程式 (FirstP.java) */ class FirstP{ public static void main(String args[]){ System.out.println("Whatever.
第 13 章 常數、靜態成員與朋友關 係 13-1 常數物件與成員 13-1 常數物件與成員 13-2 物件成員與巢狀類別 13-2 物件成員與巢狀類別 13-3 靜態成員資料與函數 13-3 靜態成員資料與函數 13-4 朋友關係 13-4 朋友關係.
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
第三章 變數與繫結 陳維魁 博士 儒林圖書公司. 2 大綱  變數的定義  變數元件  儲存區配置問題  參考的透明性  完全計算  捷徑計算  繫結 (binding)  繫結時間  精選習題.
Monte Carlo Simulation Part.2 Metropolis Algorithm Dept. Phys. Tunghai Univ. Numerical Methods C. T. Shih.
JAVA 程式設計與資料結構 第十章 GUI Introdution III. File Chooser  File Chooser 是一個選擇檔案的圖形介面, 無論我們是要存檔還是要開啟檔案,使 用這個物件都會讓我們覺得容易且舒適。
Introduction to Java Programming Lecture 5: Using Java Classes : String & Math Spring 2009.
Introduction to Java Programming Lecture 15 Objects and Classes.
Chapter 13 塑模靜態觀點:物件圖 Static View : Object Diagram.
Introduction to Java Programming Lecture 17 Abstract Classes & Interfaces.
第 5 章 深入 Response 物件 製作. 網頁的轉向與強制輸出 - 讓網頁轉彎的 Redirect 敘述 運用 Response 物件的 Redirect 方法,將瀏覽器顯 示的網頁,導向至其他網頁,語法如下: Response.Redirect 網頁路徑與名稱 此網頁路徑與名稱  若是導向到同一台.
: The largest Clique ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11324: The largest Clique 解題者:李重儀 解題日期: 2008 年 11 月 24 日 題意: 簡單來說,給你一個 directed.
第三部分:研究設計 ( 二): 研究工具的信效度 與研究效度 (第九章之第 306 頁 -308 頁;第四章)
3-3 使用幾何繪圖工具 Flash 的幾何繪圖工具包括線段工具 (Line Tool) 、橢圓形工具 (Oval Tool) 、多邊星形 工具 (Rectangle Tool) 3 種。這些工具畫出 來的幾何圖形包括了筆畫線條和填色區域, 將它們適當地組合加上有技巧地變形與配 色, 不但比鉛筆工具簡單,
Chapter 20 塑模動態觀點:狀態圖 Statechart Diagram. 學習目標  說明狀態圖的目的  定義狀態圖的基本記號  展示狀態圖的建構  定義活動、內部事件及遞延事件的狀態 圖記號.
: Fast and Easy Data Compressor ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10043: Fast and Easy Data Compressor 解題者:葉貫中 解題日期: 2007 年 3.
資料庫程式設計與系統管理 SQL Server 2005 Express 第六章 進階資料庫設計.
第十章 再論結構. 指標與結構的關係 /* File name: ex10-1a.c */ #include int main() { struct student { char *name; int score; }; struct student st= {"Brian", 97}; struct.
: Ahoy, Pirates! ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11402: Ahoy, Pirates! 解題者:李重儀 解題日期: 2008 年 8 月 26 日 題意:有一個海盜島有 N 個海盜,他們的編號 (id)
Fugacity Coefficient and Fugacity
: Multisets and Sequences ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11023: Multisets and Sequences 解題者:葉貫中 解題日期: 2007 年 4 月 24 日 題意:在這個題目中,我們要定義.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
資料結構實習-一 參數傳遞.
6-2 認識元件庫與內建元件庫 Flash 的元件庫分兩種, 一種是每個動畫專 屬的元件庫 (Library) ;另一種則是內建元 件庫 (Common Libraries), 兩者皆可透過 『視窗』功能表來開啟, 以下即為您說明。
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
函式 Function Part.2 東海大學物理系‧資訊教育 施奇廷. 遞迴( Recursion ) 函式可以「呼叫自己」,這種動作稱為 「遞迴」 此程式的執行結果相當於陷入無窮迴圈, 無法停止(只能按 Ctrl-C ) 這給我們一個暗示:函式的遞迴呼叫可以 達到部分迴圈的效果.
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
第 17 章 樣板 17-1 樣板的基礎 17-1 樣板的基礎 17-2 樣板函數 17-2 樣板函數 17-3 樣板類別 17-3 樣板類別 17-4 非型態參數的樣板類別 17-4 非型態參數的樣板類別 17-5 樣板類別的繼承 17-5 樣板類別的繼承 17-6 特化樣板與靜態成員 17-6 特化樣板與靜態成員.
Chapter 10 m-way 搜尋樹與B-Tree
: Function Overloading ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11032:Function Overloading 解題者:許智祺 解題日期: 2007 年 5 月 8 日 題意:判對輸入之數字是否為.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
5 重複迴圈 5.1 增減運算符號 增量運算符號 減量運算符號
:Commandos ★★★☆☆ 題組: Contest Archive with Online Judge 題號: 11463: Commandos 解題者:李重儀 解題日期: 2008 年 8 月 11 日 題意: 題目會給你一個敵營區內總共的建築物數,以及建築物 之間可以互通的路有哪些,並給你起點的建築物和終點.
函式 Function 東海大學物理系‧資訊教育 施奇廷. 函式簡介 當程式越來越大、越複雜時,程式的維護、 除錯會變得更困難,此時必須引入函式來 簡化程式或將程式分段,將程式重複的部 分改寫為函式,將程式「模組化」 這種作法有下列優點:節省程式發展的時 間、邏輯容易瞭解、程式容易除錯、可分 工合作完成程式.
Visual C++重點複習.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2008.
第 5 章 選擇控制項與條件敘述 5-1 結構化程式設計 5-1 結構化程式設計 5-2 關係與邏輯運算子 5-2 關係與邏輯運算子 5-3 簡單的條件敘述 5-3 簡單的條件敘述 5-4 選擇控制項 5-4 選擇控制項 5-5 巢狀條件敘述 5-5 巢狀條件敘述 5-6 多選一條件敘述 5-6 多選一條件敘述.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
What is static? CS340100, NTHU Yoshi. Static? 靜態 ? class Test { static int staticX; int instanceX; public Test(int var1, int var2) { this.staticX = var1;
Introduction to Java Programming Lecture 16 Super and Sub Classes.
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
著作權所有 © 旗標出版股份有限公司 第 14 章 製作信封、標籤. 本章提要 製作單一信封 製作單一郵寄標籤.
:Rings and Glue ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10301: Rings and Glue 解題者:施博修 解題日期: 2011 年 5 月 18 日 題意:小約翰有了個大麻煩,他不小心將 rings.
幼兒行為觀察與記錄 第八章 事件取樣法.
1 Introduction to Java Programming Lecture 3 Mathematical Operators Spring 2009.
CH 14-可靠度工程之數學基礎 探討重點 失效時間之機率分配 指數模式之可靠度工程.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
Presentation transcript:

Chapter 8 A Second Look at Classes and Objects

SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)

main Rectangle kitchen = new Rectangle (); // line 17 物件區 number totalArea input kitchen bedroom den length width

main Rectangle bedroom = new Rectangle (); // line 18 物件區 length width number totalArea input kitchen bedroom den length width

main Rectangle den = new Rectangle (); // line 19 物件區 length width number totalArea input kitchen bedroom den length width length width

main kitchen.setWidth(number); // line 29 物件區 10.0length 14.0width 14.0number totalArea input kitchen bedroom den length width length width Double parse Doubl e “14” setWidth 14.0 每個物件有自己的欄位 方法只作用在「某個物件」的欄位

main bedroom.setLength(number); // line 35 物件區 10.0length 14.0width 14.0number totalArea input kitchen bedroom den 15.0length width length width Double parse Doubl e “14” 15.0 setLength 每個物件有自己的欄位 方法只作用在「某個物件」的欄位

main totalArea = kitchen.getArea() + bedroom.getArea() + den.getArea(); // line 物件區 10.0length 14.0width 30.0number totalArea input kitchen bedroom den 15.0length 12.0width 20.0length 30.0width “30” getArea getArea

main totalArea = kitchen.getArea() + bedroom.getArea() + den.getArea(); // line 物件區 10.0length 14.0width 30.0number 920.0totalArea input kitchen bedroom den 15.0length 12.0width 20.0length 30.0width “30” getArea getArea

問題討論 新題目 – 條件 1: 程式必須印出長方形的個數 – 條件 2: 由 Rectangle.java 提供計數功能 Open – Labs\RoomAreas.java – Labs\RectangleOld.java Alice’s 解決方法 – // Alice: in RoomAreas.java – 符合條件 1 Bob’s 解決方法 – // Bob: in two java files – 符合條件 1 & 2?

Non-static vs. Static Why Bob fails? – See ppt. 12 – 物件欄位:每個物件均有一份欄位 物件方法兩特點 ( 課本 p. 522) – Compile Lab8_0.java – Compile Lab8_0_1.java – Ex: length() in String class; getAge() in Dog class Bob 應該使用 static – 修改程式 – See ppt

main Bob 的方法 物件區 10.0 length width setLe ngth number totalArea input kitchen bedroom den 1count 15.0 length width 1count 20.0 length width 1count setLe ngth getC ount 1

main 正確的方法 —static 的使用 static private int count; 物件區 number totalArea input kitchen bedroom den 0 count length width length width length width 屬於整個 Rectangle 類別

main 正確的方法 —static 的使用 kitchen.setLength(number); 物件區 number totalArea input kitchen bedroom den 1count length width length width 10.0 length width

main 正確的方法 —static 的使用 bedroom.setLength(number); 物件區 number totalArea input kitchen bedroom den 2count length width 15.0 length width 10.0 length width

main 正確的方法 —static 的使用 den.setLength(number); 物件區 number totalArea input kitchen bedroom den 3count 20.0 length width 15.0 length width 10.0 length width

main 正確的設計 —static 的使用 kitchen.getCount() 物件區 number totalArea input kitchen bedroom den 3count 20.0 length width 15.0 length width 10.0length width getC ount 3

Static 靜態欄位特性  (p. 522) – Run Lab8_1.java 靜態欄位特性  (p. 522) – Run Lab8_1_1.java Run Countable.java (p. 522) & StaticDemo.java (p. 523) – 修改 StaticDemo.java (#19) object1  object3 object1  Countable 

Static Method 靜態方法:加上 static – Countable.java getInstanceCount 改為靜態方法 – StaticDemo.java: 修改 #19 用途 – 工具型類別提供服務 Run Metric.java (p. 525) & MetricDemo.java (p. 526) See ppt – 操作靜態欄位 RectangleOld.java: static getCount() 2count 10.0 length width 靜態方法限制 – Compile Lab8_2.java, Lab8_2_1.java, Lab8_2_2.java, Lab8_2_3.java 10.0 length width  Rectangle 類別

METRIC.JAVA & METRICDEMO.JAVA ( 課本 pp )

main Line 17 (MetricDemo.java, p. 526) DecimalFormat fmt = new DecimalFormat("0.00"); 物件區 input miles kilos fmt Show Mess ageDi alog ShowI nputD ialog JOptionPane class mToKkToM Metric class Double class Decimal Format class “0.00” Decima lFormat for mat parse Doub le

main Line 21 (MetricDemo.java) 物件區 input miles kilos fmt Show Messa geDial og ShowI nputD ialog JOptionPane class mToKkToM Metric class Double class Decimal Format class “0.00” Decimal Format format “10” parse Doubl e

main Line 23 (MetricDemo.java) miles = Double.parseDouble(input); 物件區 input 10.0 miles kilos fmt Show Messa geDial og ShowI nputD ialog JOptionPane class mToKkToM Metric class Double class Decimal Format class “0.00” Decimal Format format “10” parse Doubl e

main Line 26 (MetricDemo.java) kilos = Metric.milesToKilometers(miles); 物件區 input 10.0 miles kilos fmt Show Messa geDial og ShowI nputD ialog JOptionPane class mToKkToM Metric class Double class Decimal Format class “0.00” Decimal Format format “10” parse Doubl e 10.0

Static 效果 2count 10.0 length 4 weight 10.0 length 2 weight amanda In main – Dog.length // 哪個 ?  – amanda.length // – Dog.count // – amanda.count // bob Dog main

Static 效果 2count 10.0 length 4 weight 10.0 length 2 weight amanda In M1 – length 執行哪個 M1 ,就用哪 個 length – count 就是靜態欄位 count 只有一個 bob Dog M1 M1M1

Static 效果 2count 10.0 length 4 weight 10.0 length 2 weight amanda  In M2 – length 用哪個 length?  – count 就是靜態欄位 count 只有一個 bob Dog M2

SECTION 8.4 THE TOSTRING METHOD (p. 533)

toString Run Lab8_3.java String toString() – 回傳物件狀態的標準方法 – UML: See Figure 8-8 (p. 534) – 執行時機  ( 課本 p. 536) Run Stock Class Phase 1 \Stock.java (p. 535) & StockDemo1.java (p. 537) 課堂實作 – 請將 Lab8_3.java 中的 Dog 類別增添 toString 功能 – 請用「狗狗姓名 + + 狗狗年齡」代表物件字串

SECTION 8.5 WRITING AN EQUALS METHOD (p. 537)

比較物件 Run Labs\BadStockCompare.java & Stock.java 利用 == 運算子比較 – See ppt – 比較物件的記憶體位置 比較物件的欄位內容 boolean equals(Stock o) – Run Stock Class Phase 2\StockCompare.java (p. 539) – See ppt – 比較邏輯 ( 課本 p. ?)

main Stock company1 = new Stock("XYZ", 9.62); // line 12 物件區 company1 company2 symbol sharePrice “XYZ”

main Stock company2 = new Stock("XYZ", 9.62); // line 13 物件區 company1 company2 symbol sharePrice “XYZ” symbol sharePrice “XYZ”

main if (company1 == company2) // line 17 物件區 company1 company2 symbol sharePrice “XYZ” symbol sharePrice “XYZ”

STOCKCOMPARE.JAVA (p. 539)

main if (company1.equals(company2)) // line 15 物件區 company1 company2 symbol sharePrice “XYZ” symbol sharePrice “XYZ” equals 主程式之所以知道要呼叫 company1.equals(company2) ,是因為: (1)company1 與 company2 都屬於 Stock 類別 (2)Stock 類別提供了 equals 方法可供比較 類別設計者為何寫 equals(Stock object2) : (1) 提供比較服務 (2) 傳入另一物件與方法所啟動的物件做比較

main if (symbol.equals(object2.symbol) && // line 73 of Stock.java 物件區 company1 company2 symbol sharePrice “XYZ” symbol sharePrice “XYZ” equ als object2 status 在這個當下 (line 73 of Stock.java) ,請問程式可 以存取哪幾區的記憶體? (A)company1 in main() (B) 上方物件的 symbol 欄位 (C) 下方物件的 symbol 欄位 (D)object2 in equals()

main sharePrice == object2.sharePrice // line 74 物件區 company1 company2 symbol sharePrice “XYZ” symbol sharePrice “XYZ” equ als object2 status

課堂實作 Do Algorithm Workbench 1 (pp ) – toString: 傳回半徑與面積 – equals: 面積相同則代表相同 Open Labs\CircleDemo.java 解答: LabAnswers\CircleDemo_ans.java

SECTION 8.8 THE THIS REFERENCE VARIABLE (p. 556)

main this 物件區 company1 company2 symbol 9.62 sharePrice “XYZ” this 5

可用於所有 non-static 方法中 – 試著在 Stock.java 中 #73, #74 加上 this if (this.symbol.equals(object2.symbol) && – 如果這樣呢 ? if (this.symbol.equals(this.object2.symbol) && 常見用法 – 避免遮蔽效應 (p. 557) Run Lab8_4.java – 呼叫建構式 (p. 558) Run Lab8_5.java 相關限制 ( 課本 p. 558) Compile Lab8_6b.java, Lab8_6a.java

8.7 AGGREGATION (P. 543) 到目前為止,我們自己寫的類別,都是用來修飾區域變數的型態 …

課本 p. 551

Aggregation Read Instructor.java ( 課本 p. 544) – 建構式的兩種不同寫法 Read TextBook.java ( 課本 p. 546) Read Course.java ( 課本 p. 548) – 建構式 instructor = new Instructor(instr); textBook = new TextBook(text); – getInstructor() return new Instructor(instructor); – toString(): 4 個  \n  符號為何印出 9 行 ? Read CourseDemo.java ( 課本 p. 550)

main Course myCourse = new Course("Intro to Java", myInstructor, // (p. 550) myTextBook); myInstructor myTextBook 4 “Kramer” “Shawn” “RH3010” 4 “Starting…” “Gaddis” “Addison…” myCourse courseName 4 instructor textBook Course

(String, Instructor, TextBook) Course myCourse = new Course("Intro to Java", myInstructor, myTextBook); "Intro to Java” name instr 4 “Kramer” “Shawn” “RH3010” 4 “Starting…” “Gaddis” “Addison…” text courseName 4 instructor textBook Course

(String, Instructor, TextBook) courseName = name; "Intro to Java” name instr 4 “Kramer” “Shawn” “RH3010” 4 “Starting…” “Gaddis” “Addison…” text "Intro to Java” courseName 4 instructor textBook Course

(String, Instructor, TextBook) instructor = new Instructor(instr); "Intro to Java” name instr 3 “Kramer” “Shawn” “RH3010” 4 “Starting…” “Gaddis” “Addison…” text "Intro to Java” courseName 4 instructor textBook Course Instruc tor 3

Instructor (Instructor) lastName = object2.lastName; firstName = object2.firstName; officeNumber = object2.officeNumber; object2 3 “Kramer” “Shawn” “RH3010” 4 “Starting…” “Gaddis” “Addison…” "Intro to Java” courseName 4 instructor textBook Course Instruc tor 3 “Kramer” “Shawn” “RH3010” Instruc tor

Instructor (Instructor) } object2 3 “Kramer” “Shawn” “RH3010” 4 “Starting…” “Gaddis” “Addison…” "Intro to Java” courseName 4 instructor textBook Course Instruc tor 3 “Kramer” “Shawn” “RH3010” Instruc tor Return to …

Course (String, Instructor, TextBook) instructor = new Instructor(instr); // #27 (p. 548) "Intro to Java” name instr 4 “Kramer” “Shawn” “RH3010” 4 “Starting…” “Gaddis” “Addison…” text "Intro to Java” courseName 4 instructor textBook Course 3 “Kramer” “Shawn” “RH3010” Instruc tor

Course (String, Instructor, TextBook) textBook = new TextBook(text); // #31 (p. 548) "Intro to Java” name instr 4 “Kramer” “Shawn” “RH3010” 4 “Starting…” “Gaddis” “Addison…” text "Intro to Java” courseName 4 instructor textBook 4 “Kramer” “Shawn” “RH3010” “Starting…” “Gaddis” “Addison…” 4 Course

(String, Instructor, TextBook) } // #32 (p. 548) "Intro to Java” name instr 4 “Kramer” “Shawn” “RH3010” 4 “Starting…” “Gaddis” “Addison…” text "Intro to Java” courseName 4 instructor textBook 4 “Kramer” “Shawn” “RH3010” “Starting…” “Gaddis” “Addison…” 4 Return to … main Course

main Course myCourse = // #19 (p. 550) new Course("Intro to Java", myInstructor, myTextBook); myInstructor myTextBook 4 “Kramer” “Shawn” “RH3010” 4 “Starting…” “Gaddis” “Addison…” myCourse 4 “Kramer” “Shawn” “RH3010” “Starting…” “Gaddis” “Addison…” 4 "Intro to Java” courseName 5 instructor textBook

Aggregation Run Lab8_7.java – 請注意 #25 – myInstructor 與 myCourse 所包含的 instructor 是同 一物件 ?

課堂實作 續 Lab8_7.java 請修改 Course.java 的某一行 讓輸出結果改成: 解答請見 LabAnswers\Course_ans.java

課堂實作 Open Mate\Dog.java, Cat.java, MateDemo.java, Mate.java 狗狗跟貓咪可以配成好友 (Mate) 已知 MateDemo.java 執行後印出:

課堂實作 請寫出 Mate 類別的各方法 – public Mate(Dog d, Cat c) 複製狗狗 d 與貓咪 c ,成為 Mate 物件的欄位變數 – public String toString() 印出 “ 狗狗 : 狗狗名字 ( 年齡 ) 跟貓咪 : 貓咪名字 ( 年齡 ) 是 好朋友 " – public void changeMate(Dog d) 複製狗狗 d 給 dogMate 欄位變數 – public void changeMate(Cat c) 複製貓咪 c 給 catMate 欄位變數 解答請見 LabAnswers\Mate_ans.java

SECTION 8.9 ENUMERATED TYPES 課本 p. 559

要怎麼表示星期幾 int day = 0; // 星期天 int day1 = 1; // 星期一 int day2 = 2; // 星期二 … int dayN = 8; // 星期 ?

列舉型態 (enumerated type) 定義型態 ( 課本 P. 559) enum Day {SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY} 宣告變數 Day workday; 指定型態值 Workday = Day.THURSDAY;

列舉型態 (enumerated type) 列舉型態值是個物件 – See Figure 8-15 ( 課本 p. 560) Run EnumDemo.java ( 課本 p. 562) – 假設 X 是一個列舉型態參考變數 ( 物件 ) ,則 – System.out.println(X); // 印出列舉型態值 – X.ordinal()  X 參考的列舉型態值的順位 Run SportsCarDemo.java (p. 565) – (with SportsCar.java, CarType.java, CarColor.java) Run SportsCarDemo2.java (p. 566) – 注意 case 敘述後不接列舉型態 (CarType)

SECTION 9.2 PASSING OBJECTS AS ARGUMENTS TO METHODS

PASSOBJECT.JAVA(P. 570) Section 9.2

main Rectangle box = new Rectangle (12.0, 5.0); // line 10 物件區 box 12.0length 5.0width

main displayRectangle (r); // line 14 物件區 box 12.0length 5.0width displayRectangle r

main System.out.println(“Length : “ + r.getLength() + “Width : “ + r.getWidth()); // line 物件區 box 12.0length 5.0width getLe ngth displayRectangle r

PASSOBJECT2.JAVA(P. 572) Section 9.2

main Rectangle box = new Rectangle (12.0, 5.0); // line 11 物件區 box 12.0length 5.0width

main displayRectangle (r); // line 20 物件區 box 12.0length 5.0width displayRectangle r

main r.setLength(0.0) ; // line 37 物件區 box 0.0length 5.0width setLe ngth displayRectangle r 0.0

main r.setWidth(0.0) ; // line 38 物件區 box 0.0length 0.0width setW idth displayRectangle r 0.0 把物件傳入方法 可以改變物件欄位值

SECTION 9.3 RETURNING OBJECTS FROM METHODS

RETURNOBJECT.JAVA(P. 573) Section 9.3

main account = getAccount(); // line 15 物件區 account

main return new BankAccount(balance); // line 44 物件區 account balance getAccount input balance “ ”

main return new BankAccount(balance); // line 44 account = getAccount(); // line 15 account getAccount input balance 物件區 balance “ ”