![Page 1: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/1.jpg)
Chapter 8 A Second Look at Classes and Objects
![Page 2: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/2.jpg)
SECTION 8.1 STATIC CLASS MEMBERS
RoomAreas.java( 課本 p. 372)
Rectangle.java( 課本 p. 366)
![Page 3: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/3.jpg)
main
Rectangle kitchen = new Rectangle (); // line 17
物件區
numbertotalArea
inputkitchen
bedroomden
length
width
![Page 4: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/4.jpg)
main
Rectangle bedroom = new Rectangle ();// line 18
物件區
length
widthnumber
totalAreainput
kitchenbedroom
den
length
width
![Page 5: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/5.jpg)
main
Rectangle den = new Rectangle (); // line 19
物件區
length
widthnumber
totalAreainput
kitchenbedroom
den
length
width
length
width
![Page 6: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/6.jpg)
main
kitchen.setWidth(number); // line 29
物件區
10.0length
14.0width14.0number
totalAreainput
kitchenbedroom
den
length
width
length
width
Double
parseDoubl
e
“14”
setWidth
14.0
每個物件有自己的欄位方法只作用在「某個物件」的欄位
![Page 7: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/7.jpg)
main
bedroom.setLength(number); // line 35
物件區
10.0length
14.0width14.0number
totalAreainput
kitchenbedroom
den
15.0length
width
length
width
Double
parseDoubl
e
“14”
15.0
setLength
每個物件有自己的欄位方法只作用在「某個物件」的欄位
![Page 8: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/8.jpg)
main
totalArea = kitchen.getArea() + bedroom.getArea() + den.getArea();// line 52-53
物件區
10.0length
14.0width30.0number
totalAreainput
kitchenbedroom
den
15.0length
12.0width
20.0length
30.0width
“30”
getArea140.0
getArea
getArea
180.0
600.0
![Page 9: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/9.jpg)
main
totalArea = kitchen.getArea() + bedroom.getArea() + den.getArea();// line 52-53
物件區
10.0length
14.0width30.0number
920.0totalAreainput
kitchenbedroom
den
15.0length
12.0width
20.0length
30.0width
“30”
getArea140.0
getArea
getArea
180.0
600.0+
+
![Page 10: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/10.jpg)
問題討論• 新題目
– 條件 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?
![Page 11: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/11.jpg)
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. 13-17
![Page 12: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/12.jpg)
main
Bob 的方法 物件區
10.0length
width
setLength
numbertotalArea
inputkitchen
bedroomden
1count
15.0length
width1count
20.0length
width1count
setLength
setLength
getCount
1
![Page 13: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/13.jpg)
main
正確的方法— static 的使用static private int count; 物件區
numbertotalArea
inputkitchen
bedroomden
0count
length
width
length
width
length
width
屬於整個 Rectangle類別
![Page 14: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/14.jpg)
main
正確的方法— static 的使用 kitchen.setLength(number); 物件區
numbertotalArea
inputkitchen
bedroomden
1count
length
width
length
width
10.0length
width
![Page 15: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/15.jpg)
main
正確的方法— static 的使用bedroom.setLength(number); 物件區
numbertotalArea
inputkitchen
bedroomden
2count
length
width
15.0length
width
10.0length
width
![Page 16: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/16.jpg)
main
正確的方法— static 的使用den.setLength(number); 物件區
numbertotalArea
inputkitchen
bedroomden
3count
20.0length
width
15.0length
width
10.0length
width
![Page 17: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/17.jpg)
main
正確的設計— static 的使用kitchen.getCount() 物件區
numbertotalArea
inputkitchen
bedroomden
3count
20.0length
width
15.0length
width
10.0length
widthgetCount
3
![Page 18: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/18.jpg)
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
![Page 19: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/19.jpg)
Static Method
• 靜態方法:加上 static– Countable.java
• getInstanceCount 改為靜態方法– StaticDemo.java: 修改 #19
• 用途– 工具型類別提供服務
• Run Metric.java (p. 525) &MetricDemo.java (p. 526)
• See ppt. 20-4
– 操作靜態欄位• RectangleOld.java: static getCount()
2count
10.0length
width
• 靜態方法限制– Compile Lab8_2.java, Lab8_2_1.java, Lab8_2_2.java, Lab8_2_3.java
10.0length
width
Rectangle 類別
![Page 20: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/20.jpg)
METRIC.JAVA & METRICDEMO.JAVA( 課本 pp. 567-8)
![Page 21: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/21.jpg)
main
Line 17 (MetricDemo.java, p. 526)
DecimalFormat fmt = new DecimalFormat("0.00");
物件區
inputmileskilosfmt
ShowMessageDialog
ShowInputDialog
JOptionPane class
mToKkToM
Metric class
Double class
DecimalFormatclass
“0.00”
DecimalFormat
format
parseDoub
le
![Page 22: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/22.jpg)
main
Line 21 (MetricDemo.java) 物件區
inputmileskilosfmt
ShowMessageDial
og
ShowInputDialog
JOptionPane class
mToKkToM
Metric class
Double class
DecimalFormatclass
“0.00”
DecimalFormat format
“10”
parseDoubl
e
![Page 23: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/23.jpg)
main
Line 23 (MetricDemo.java)
miles = Double.parseDouble(input); 物件區
input10.0miles
kilosfmt
ShowMessageDial
og
ShowInputDialog
JOptionPane class
mToKkToM
Metric class
Double class
DecimalFormatclass
“0.00”
DecimalFormat format
“10”
parseDoubl
e
![Page 24: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/24.jpg)
main
Line 26 (MetricDemo.java)
kilos = Metric.milesToKilometers(miles); 物件區
input10.0miles
16.09kilosfmt
ShowMessageDial
og
ShowInputDialog
JOptionPane class
mToKkToM
Metric class
Double class
DecimalFormatclass
“0.00”
DecimalFormat format
“10”
parseDoubl
e
10.0
![Page 25: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/25.jpg)
Static 效果
2count
10.0length
4weight
10.0length
2weightamanda
• In main– Dog.length // 哪個 ? – amanda.length // – Dog.count // – amanda.count //
bob
Dog
main
![Page 26: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/26.jpg)
Static 效果
2count
10.0length
4weight
10.0length
2weightamanda
• In M1– length • 執行哪個 M1 ,就用哪
個 length
– count • 就是靜態欄位 count• 只有一個
bob
Dog
M1
M1
![Page 27: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/27.jpg)
Static 效果
2count
10.0length
4weight
10.0length
2weightamanda
• In M2– length • 用哪個 length?
– count • 就是靜態欄位 count• 只有一個 bob
Dog
M2
![Page 28: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/28.jpg)
SECTION 8.4 THE TOSTRING METHOD
(p. 533)
![Page 29: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/29.jpg)
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 功能– 請用「狗狗姓名 + “@” + 狗狗年齡」代表物件字串
![Page 30: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/30.jpg)
SECTION 8.5 WRITING AN EQUALS METHOD
(p. 537)
![Page 31: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/31.jpg)
比較物件• Run Labs\BadStockCompare.java & Stock.java• 利用 == 運算子比較– See ppt. 32-4– 比較物件的記憶體位置
• 比較物件的欄位內容• boolean equals(Stock o)– Run Stock Class Phase 2\StockCompare.java(p. 539)
– See ppt. 36-8– 比較邏輯 ( 課本 p. ?)
![Page 32: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/32.jpg)
main
Stock company1 = new Stock("XYZ", 9.62); // line 12
物件區
company1company2
symbol
5
9.62sharePrice
“XYZ”
![Page 33: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/33.jpg)
main
Stock company2 = new Stock("XYZ", 9.62); // line 13
物件區
company1company2
symbol
5
9.62sharePrice
“XYZ”
symbol
5
9.62sharePrice
“XYZ”
![Page 34: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/34.jpg)
main
if (company1 == company2) // line 17
物件區
company1company2
symbol
5
9.62sharePrice
“XYZ”
symbol
5
9.62sharePrice
“XYZ”
![Page 35: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/35.jpg)
STOCKCOMPARE.JAVA(p. 539)
![Page 36: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/36.jpg)
main
if (company1.equals(company2)) // line 15
物件區
company1company2
symbol
4
9.62sharePrice
“XYZ”
symbol
5
9.62sharePrice
“XYZ”
equals
主程式之所以知道要呼叫company1.equals(company2) ,是因為:(1)company1 與 company2 都屬於 Stock 類別(2)Stock 類別提供了 equals 方法可供比較
類別設計者為何寫 equals(Stock object2) :(3) 提供比較服務(4) 傳入另一物件與方法所啟動的物件做比較
![Page 37: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/37.jpg)
main
if (symbol.equals(object2.symbol) && // line 73 of Stock.java
物件區
company1company2
symbol
4
9.62sharePrice
“XYZ”
symbol
5
9.62sharePrice
“XYZ”
equals
equals object2status
在這個當下 (line 73 of Stock.java) ,請問程式可以存取哪幾區的記憶體?
(A) company1 in main()(B) 上方物件的 symbol 欄位(C) 下方物件的 symbol 欄位(D) object2 in equals()
![Page 38: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/38.jpg)
main
sharePrice == object2.sharePrice // line 74
物件區
company1company2
symbol
4
9.62sharePrice
“XYZ”
symbol
5
9.62sharePrice
“XYZ”
equals
object2status
![Page 39: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/39.jpg)
課堂實作• Do Algorithm Workbench 1 (pp. 577-8)– toString: 傳回半徑與面積– equals: 面積相同則代表相同
• Open Labs\CircleDemo.java• 解答: LabAnswers\CircleDemo_ans.java
![Page 40: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/40.jpg)
SECTION 8.8 THE THIS REFERENCE VARIABLE
(p. 556)
![Page 41: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/41.jpg)
main
this物件區
company1company2
symbol
9.62sharePrice
“XYZ”
this5
![Page 42: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/42.jpg)
this
• 可用於所有 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
![Page 43: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/43.jpg)
8.7 AGGREGATION(P. 543)
到目前為止,我們自己寫的類別,都是用來修飾區域變數的型態…
![Page 44: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/44.jpg)
課本 p. 551
![Page 45: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/45.jpg)
Aggregation
• Read Instructor.java ( 課本 p. 544)
– 建構式的兩種不同寫法• Read TextBook.java ( 課本 p. 546)
• Read Course.java ( 課本 p. 548)
– 建構式• instructor = new Instructor(instr); (#27 @Course.java)• textBook = new TextBook(text); (#31 @Course.java)
– getInstructor()• return new Instructor(instructor); (#53 @Course.java)
– toString(): 4 個 \n 符號為何印出 9 行 ?• Read CourseDemo.java ( 課本 p. 550)
![Page 46: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/46.jpg)
main
Course myCourse = new Course("Intro to Java", myInstructor, // #20 @CourseDemo.java (p. 550) myTextBook);
myInstructor
myTextBook
4
“Kramer”
“Shawn”
“RH3010”
4
“Starting…”
“Gaddis”“Addison…
”
myCourse
courseName
4
instructor
textBookCourse
![Page 47: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/47.jpg)
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
textBookCourse
![Page 48: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/48.jpg)
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
textBookCourse
![Page 49: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/49.jpg)
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
textBookCourse
Instructor
3Instructor
![Page 50: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/50.jpg)
Instructor (Instructor)
lastName = object2.lastName;firstName = object2.firstName;officeNumber = object2.officeNumber;
object23
“Kramer”
“Shawn”
“RH3010”
4
“Starting…”
“Gaddis”“Addison…
”
"Intro to Java”courseName
4
instructor
textBookCourse
Instructor
3
“Kramer”
“Shawn”
“RH3010”
Instructor
![Page 51: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/51.jpg)
Instructor (Instructor)
}
object23
“Kramer”
“Shawn”
“RH3010”
4
“Starting…”
“Gaddis”“Addison…
”
"Intro to Java”courseName
4
instructor
textBookCourse
Instructor
3
“Kramer”
“Shawn”
“RH3010”
Instructor
Return to …
![Page 52: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/52.jpg)
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
textBookCourse
3
“Kramer”
“Shawn”
“RH3010”
Instructor
![Page 53: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/53.jpg)
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
textBook4
“Kramer”
“Shawn”
“RH3010”
“Starting…”
“Gaddis”“Addison…
”4
Course
![Page 54: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/54.jpg)
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
textBook4
“Kramer”
“Shawn”
“RH3010”
“Starting…”
“Gaddis”“Addison…
”4
Return to …main
Course
![Page 55: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/55.jpg)
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
![Page 56: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/56.jpg)
Aggregation
• Run Lab8_7.java– 請注意 #25– myInstructor 與 myCourse 所包含的 instructor 是
同一物件 ?
![Page 57: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/57.jpg)
課堂實作• 續 Lab8_7.java• 請修改 Course.java 的某一行• 讓輸出結果改成:
• 解答請見 LabAnswers\Course_ans.java
![Page 58: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/58.jpg)
課堂實作• Open Mate\Dog.java, Cat.java,
MateDemo.java, Mate.java• 狗狗跟貓咪可以配成好友 (Mate)• 已知 MateDemo.java 執行後印出:
![Page 59: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/59.jpg)
課堂實作• 請寫出 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
![Page 60: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/60.jpg)
SECTION 8.9 ENUMERATED TYPES課本 p. 559
![Page 61: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/61.jpg)
要怎麼表示星期幾• int day = 0; // 星期天• int day1 = 1; // 星期一• int day2 = 2; // 星期二• …• int dayN = 8; // 星期 ?
![Page 62: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/62.jpg)
列舉型態 (enumerated type)
• 定義型態 ( 課本 P. 559)
enum Day {SUNDAY, MONDAY , TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY}
• 宣告變數Day workday;
• 指定型態值Workday = Day.THURSDAY;
![Page 63: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/63.jpg)
列舉型態 (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)
![Page 64: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/64.jpg)
SECTION 9.2 PASSING OBJECTS AS ARGUMENTS TO METHODS
![Page 65: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/65.jpg)
PASSOBJECT.JAVA(P. 570)Section 9.2
![Page 66: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/66.jpg)
main
Rectangle box = new Rectangle (12.0, 5.0); // line 10
物件區
box
12.0length
5.0width
![Page 67: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/67.jpg)
main
displayRectangle (r); // line 14
物件區
box
12.0length
5.0width
displayRectangler
![Page 68: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/68.jpg)
main
System.out.println(“Length : “ + r.getLength() + “Width : “ + r.getWidth()); // line 27-28
物件區
box
12.0length
5.0width
getLength
displayRectangler
![Page 69: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/69.jpg)
PASSOBJECT2.JAVA(P. 572)Section 9.2
![Page 70: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/70.jpg)
main
Rectangle box = new Rectangle (12.0, 5.0); // line 11
物件區
box
12.0length
5.0width
![Page 71: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/71.jpg)
main
displayRectangle (r); // line 20
物件區
box
12.0length
5.0width
displayRectangler
![Page 72: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/72.jpg)
main
r.setLength(0.0) ; // line 37
物件區
box
0.0length
5.0width
setLength
displayRectangler
0.0
![Page 73: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/73.jpg)
main
r.setWidth(0.0) ; // line 38
物件區
box
0.0length
0.0width
setWidth
displayRectangler
0.0
把物件傳入方法可以改變物件欄位值
![Page 74: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/74.jpg)
SECTION 9.3 RETURNING OBJECTS FROM METHODS
![Page 75: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/75.jpg)
RETURNOBJECT.JAVA(P. 573)Section 9.3
![Page 76: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/76.jpg)
main
account = getAccount(); // line 15
物件區
account
![Page 77: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/77.jpg)
main
return new BankAccount(balance); // line 44
物件區
account
3200.0balance
getAccountinput
3200.0balance“3200.00”
![Page 78: Chapter 8 A Second Look at Classes and Objects. SECTION 8.1 STATIC CLASS MEMBERS RoomAreas.java ( 課本 p. 372) Rectangle.java ( 課本 p. 366)](https://reader035.vdocuments.mx/reader035/viewer/2022081420/56649ece5503460f94bda9ad/html5/thumbnails/78.jpg)
main
return new BankAccount(balance); // line 44account = getAccount(); // line 15
account
getAccountinput
balance
物件區
3200.0balance
“3200.00”