--- title: JAVA2程式筆記-第8-15章 tags: JAVA2程式筆記 --- #JAVA2程式筆記-第8-15 章 延續在https://medium.com/p/608a1b187e72/edit 的筆記因為放不上了~所以以此為連結 Chapter8類別class的基礎知識 ![](https://i.imgur.com/1CH8fp6.jpg) 點開發現有兩個CLASS ![](https://i.imgur.com/h4xeeWu.jpg) 呼叫方法void show() 還有 car1.show(); ![](https://i.imgur.com/35SsqPv.jpg) 帶有傳回值的方法 ![](https://i.imgur.com/eNqbDeo.jpg) 程式碼很長貼在這裡:https://gist.github.com/jzs2home/10dce5fbbccbf1f70465861e9502e116 --- Chapter 9 類別的功能 修飾子: private:資料不能由類別外部任意存取 public:可以從類別以外的地方存取 ![](https://i.imgur.com/WK0rfIp.jpg) 程式碼很長貼在這裡:https://gist.github.com/jzs2home/fd5599d2b58e1fb5d7288d4e43810b80 封裝=讓私有的資料受到保護,避免外在的讀取 --- 多載:同一個類別中可以同時定義3個setCar()的方法 ![](https://i.imgur.com/QaOsNKr.jpg) 程式碼: https://gist.github.com/jzs2home/bcd94498538ed219a3e55570db77ebff 建構式:幫助物件初始化 public Car(){ num=0; gas=0.0; System.out.printf("生產了車子"); } ![](https://i.imgur.com/cRmNIn9.jpg) 類別變數前加上static代表一個靜態資料 --- Chapter 10 類別的應用方式 選幾個常用的類別連結: String charAt()方法=http://tw.gitbook.net/java/java_string_charat.html length() https://medium.com/cubemail88/java-%E4%B8%AD%E7%9A%84-length-length-size-%E4%B8%80%E6%AC%A1%E6%91%B8%E9%80%8F%E9%80%8F-24b82cb41e22 toLowerCase()把字元變為小寫,toUpperCase()把字元變為大寫 --- 搜尋文字indexOf()裡面一定要放ch 字串中的第1個字是0後面的輸出使用num+1/找到字元會傳回-1 找不到時會回復錯誤 ![](https://i.imgur.com/bFclIaC.jpg) 字串的連接:StringBuffer和append() ![](https://i.imgur.com/98pnb0y.jpg) math類別 ![](https://i.imgur.com/B5JLCiH.jpg) --- Chapter 11 建立新類別-繼承(inheritance) extends->類別的延伸 子類別(subclass)名稱 extends 父類別(superclass)名稱 新增setCourse方法 ![](https://i.imgur.com/eIXNtz5.jpg) 程式碼: https://gist.github.com/jzs2home/888b168f9012d6f4d8e5b58ee8a89456 super()從子類別呼叫父類別(要注意public要加入extends) ![](https://i.imgur.com/G4uV1iB.jpg) 程式碼:https://gist.github.com/jzs2home/ec5c9d7e1248626c4014aa0403da8adb this()呼叫同一類別內部建構式(要注意private沒有void只有一個.show) ![](https://i.imgur.com/KpG8wVY.jpg) 程式碼:https://gist.github.com/jzs2home/b65a859132bcbf3402083cb3c037d8d9 super()和this()不能同時用 --- 保護成員protected ![](https://i.imgur.com/goFUERC.jpg) 程式碼:https://gist.github.com/jzs2home/71f51014c1c8f4ba43f09e2a75da8cd8 --- toString()-注意return str; ![](https://i.imgur.com/dChhgpw.jpg) 程式碼:https://gist.github.com/jzs2home/d40ef41c90b5a87f24a5ee110752195a --- equals() ![](https://i.imgur.com/0FRhQwg.jpg) 程式碼:https://gist.github.com/jzs2home/8b5fbc80a7662e4c5f52e4e9779e92b1 --- getClass() ![](https://i.imgur.com/bNZmc9s.jpg) 程式碼:https://gist.github.com/jzs2home/898fd1a39556df4822695f26c66f6201 --- Chapter 12 介面 abstract抽象類別-vehicle(交通工具)-產生car/plane abstract class Vehicle (旁邊的圖示改變) 要記得在旁邊是要新建一個新的package不然裡面同前面有class car 在idea裡面會出現錯誤 ![](https://i.imgur.com/RL9Xsiq.jpg) 程式碼:https://gist.github.com/jzs2home/969138595176d84abf9306f70fe137e1 宣告介面時是使用interface(圖案也長的不同)而不是class ![](https://i.imgur.com/vz5AtIx.jpg) 程式碼:https://gist.github.com/jzs2home/0b5632d7712be9e9f13c098510a63e29 --- 多重繼承/兩個以上的介面 ![](https://i.imgur.com/E5ZoERb.jpg) 程式碼:https://gist.github.com/jzs2home/3a32c33466e976f02a1d809530bcce58 注意延伸介面使用extends還是implements 父介面--extends--子介面--implements--類別 --- Chapter 13 設計大型程式-入門篇-分割檔案-2個class 須把2個檔案放在同一目錄=套件(package)下 檔案1: / 檔案2: ![](https://i.imgur.com/6VqV58y.jpg) 匯入 import java.lang: 基本的核心類別,字串、執行緒、例外類別 ● java.util: 萬用工具箱 , 輔助性類別 --- Chapter 14 例外處理與輸入∕輸出處理:ArrayIndexOutBoundsException 將資料輸出到檔案/建一個txt檔 知道txt檔在哪裡非常重要(就算照KEY程式碼,這裡不會找,還是不能RUN) ![](https://i.imgur.com/xdPpGdt.png) 將資料輸出到檔案 ![](https://i.imgur.com/6mWaaeC.jpg) 程式碼:https://gist.github.com/jzs2home/221fef4b4dddcf518f76218b83c02431 --- Chapter 15 執行緒:Thread類別 避免多執行緒造成的錯誤要加入synchronized ![](https://i.imgur.com/L1rMKSZ.jpg) 程式碼:https://gist.github.com/jzs2home/e1eab65478522873359eef34cff17650 --- 後面的章節放在https://hackmd.io/@sSA6NW2ZToCxgSaVETWNCg/Bkp5_96tU --- 我所根據的書:最新Java2程式設計實例入門與活用-超值增訂第四版