從前端到全棧 -- 最全面向對象總結

{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"從事前端的這兩年中,也接觸了後端領域,像 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"PHP","attrs":{}}],"attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Node","attrs":{}}],"attrs":{}},{"type":"text","text":", 並且都用它們做過一些 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"demo","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"私活","attrs":{}}],"attrs":{}},{"type":"text","text":" ,站在職業發展道路上來講,瞭解後端是好的,自己可以熟練前後的開發流程,整條鏈路下來,很清晰,懂了後端,在自己以後創業或者接私活,都是不錯的。 而且現在在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Web","attrs":{}}],"attrs":{}},{"type":"text","text":"後臺領域, ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Java","attrs":{}}],"attrs":{}},{"type":"text","text":" 是 老大哥了,","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Spring","attrs":{}}],"attrs":{}},{"type":"text","text":" 全家桶走遍天下,於是最近又重拾 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Java","attrs":{}}],"attrs":{}},{"type":"text","text":" 了,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"從前端邁向全棧","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這是我最近花了一週的時間覆盤的 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Java 面向對象","attrs":{}}],"attrs":{}},{"type":"text","text":" 總結,希望能幫助到你.","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"導讀目錄","attrs":{}}]},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/af/af4dade71d807ba26e6f1fbbafc368dc.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"學習路線","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這份學習路線 是 我跟着 某up主 視頻總結的,很全面","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"image","attrs":{"src":"https://static001.geekbang.org/infoq/e5/e57643c3ff95cd8cbc0f2da6c6fdef6b.png","alt":null,"title":"","style":[{"key":"width","value":"100%"},{"key":"bordertype","value":"boxShadow"}],"href":"","fromPaste":false,"pastePass":false}},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"重學Java","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"一、面向對象 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"OOP","attrs":{}}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1. 1 關於 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 區別","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}},{"type":"text","text":" 調用父類的構造方法,必須在子類構造方法中第一個","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}},{"type":"text","text":" 必須只能出現在子類的方法 或者 構造方法 中","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}},{"type":"text","text":" 和 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":" this","attrs":{}}],"attrs":{}},{"type":"text","text":" 不能同時出現在調用構造方法","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"代表的對象不同:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" : 本身調用者這個對象","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"super","attrs":{}}],"attrs":{}},{"type":"text","text":" : 代表父類對象應用","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.2 多態","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"同一方法可以根據發送對象的不同而採用多種不同的行爲方式。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"一個對象的實際類型是確定的,但可以指向對象的引用類型有很多。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"多態存在條件","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"有繼承關係","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"子類重寫父類的方法","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"父類引用指向了子類對象","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"注意","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"子類如果重寫了父類方法,父類對象是調用的子類重寫後的方法","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"類型轉換","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"類型轉換目的: ","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"方便方法調用,降級,升級,減少代碼重複性,但也有缺點,丟失精度性","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"向上轉型 (子 轉 父 ) 低轉高 可直接轉","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"向下轉型 (父 轉 子) 高轉低 需要強制轉換","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":"none"},"content":[{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"高 轉 低 後, 然後再進行強制轉換,","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"子類型 子對象 = (子類型) 父對象","attrs":{}}],"attrs":{}}]}],"attrs":{}}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":" Person per = new Student()\n Student student = (Student) per // ((Student) per).方法/屬性\n \n per.子屬性和方法\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.3 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.3.1 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 用途","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在沒有創建對象的情況下調用方法和屬性(靜態的)。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 可以用來修飾類的成員方法、類的成員變量,另外可以編寫static代碼塊來優化程序性能。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.3.2 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 靜態方法","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 方法一般稱作靜態方法,靜態方法不依賴於任何對象就可以進行訪問, 因此對於靜態方法來說,是沒有this的。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"在靜態方法中","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"不能訪問類的非靜態成員變量和非靜態成員方法","attrs":{}},{"type":"text","text":",因爲非靜態成員方法/變量都是必須依賴具體的對象才能夠被調用。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"在非靜態方法中是可以訪問靜態成員方法/變量的。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.3.3 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 靜態變量","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"靜態變量和非靜態變量的區別是","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"靜態變量被所有的對象所共享,在內存中只有一個副本,它當且僅當在類初次加載時會被初始化。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"非靜態變量是對象所擁有的,在創建對象的時候被初始化,存在多個副本,各個對象擁有的副本互不影響。","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"static成員變量的初始化順序按照定義的順序進行初始化。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.3.4 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 代碼塊","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"關鍵字來形成靜態代碼塊以優化程序性能,。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"塊可以置於類中的任何地方,類中可以有多個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"塊。在類初次被加載的時候,會按照","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"塊的順序來執行每個","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"塊,並且只會執行一次。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"可以把方法中常量放入到 靜態塊中,這樣不至於,每次調用,又重新創建,造成空間浪費。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class Test {\n private static Date date ;\n private static SimpleDateFormat formatter;\n static {\n formatter= new SimpleDateFormat(\"yyyy-MM-dd 'at' HH:mm:ss z\");\n date = new Date(System.currentTimeMillis());\n }\n public static void main(String[] args) {\n getTime();\n }\n public static void getTime(){\n System.out.println(formatter.format(date));\n }\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上例子是 用來獲取當前時間,在調用","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"getTime","attrs":{}}],"attrs":{}},{"type":"text","text":" 靜態方法時,它不會重複創建","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Date","attrs":{}}],"attrs":{}},{"type":"text","text":" ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"SimpleDateFormat","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"執行順序","attrs":{}},{"type":"text","text":": 優於構造器","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.3.5 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":" 注意","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"static","attrs":{}}],"attrs":{}},{"type":"text","text":"關鍵字不能會改變類中成員的訪問權限, 能改變訪問權限只有 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"private","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public","attrs":{}}],"attrs":{}},{"type":"text","text":"、","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"protected","attrs":{}}],"attrs":{}},{"type":"text","text":".","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"通過 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 可以在非靜態方法中是可以訪問靜態方法和靜態成員的","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":" public class Test {\n private static Date date ;\n private static SimpleDateFormat formatter;\n private static String name = \"前端自學社區\";\n \n public static void main(String[] args) {\n Test test = new Test();\n test.getStatic();\n }\n public static void func(){\n System.out.println(\"我是靜態方法\");\n }\n public void getStatic(){\n System.out.println(this.name);\n this.func();\n }\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"例子中,在非靜態方法中,可以直接通過 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"this","attrs":{}}],"attrs":{}},{"type":"text","text":" 來進行訪問的靜態方法和靜態成員 ,然後通過類實例化後,對象即可通過非靜態方法來獲取到靜態方法中得靜態屬性和方法。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.4 抽象類 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"abstract","attrs":{}}],"attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"我們先來了解一下抽象方法,抽象方法是一種特殊的方法:","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"它只有聲明,而沒有具體的實現","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"抽象方法的聲明格式爲:","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"abstract int getNum();\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.4.1何爲 抽象類 呢 ?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"被 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"abstract","attrs":{}}],"attrs":{}},{"type":"text","text":"關鍵詞修飾的類就是抽象類,抽象類含有屬性和抽象方法。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"抽象類中含有無具體實現的方法,所以不能用抽象類創建對象。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當 子類 繼承 了 抽象類 ,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"那麼必須的實現抽象類的抽象方法,同時 子類的實例化對象擁有抽象類的屬性","attrs":{}},{"type":"text","text":"。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public abstract class Animal {\n abstract String name;\n abstract void eat();\n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.4.2 抽象類作用?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"抽象類主要用來抽離事物的共性,抽離出屬性和方法,子類 繼承 抽象類,提高代碼效率。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.4.3 抽象類與普通類區別","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"抽象方法必須爲public或者protected(因爲如果爲private,則不能被子類繼承,子類便無法實現該方法),缺省情況下默認爲public。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"抽象類不能用來創建對象;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"如果一個類繼承於一個抽象類,","attrs":{}},{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"則子類必須實現父類的抽象方法","attrs":{}},{"type":"text","text":"。如果子類沒有實現父類的抽象方法,則必須將子類也定義爲爲abstract類。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.5 接口 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"interface","attrs":{}}],"attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.5.1 什麼是接口?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"它是對行爲的抽象,接口通常以","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"interface","attrs":{}}],"attrs":{}},{"type":"text","text":"來聲明。一個類通過繼承接口的方式,從而來繼承接口的抽象方法。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"當一個類需要用到這些抽象行爲的話,需要 實現接口 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"implements","attrs":{}}],"attrs":{}},{"type":"text","text":" , 而且必須實現接口中方法。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public interface person {\n void study(){} \n}\n","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.5.2 接口特性","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"接口中每一個方法也是隱式抽象的,接口中的方法會被隱式的指定爲 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public abstract","attrs":{}}],"attrs":{}},{"type":"text","text":"(只能是 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public abstract","attrs":{}}],"attrs":{}},{"type":"text","text":",其他修飾符都會報錯)。 也可不寫,默認就是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public abstract","attrs":{}}],"attrs":{}},{"type":"text","text":" 。","attrs":{}}]}]}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":" public interface Aniaml{\n public abstract void eat(); // 抽象方法 == 等同於 void eat();\n }\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"numberedlist","attrs":{"start":2,"normalizeStart":2},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"接口中可以含有變量,但是接口中的變量會被隱式的指定爲 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public static final","attrs":{}}],"attrs":{}},{"type":"text","text":" 變量(並且只能是 public,用 private 修飾會報編譯錯誤)。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"接口中的方法是不能在接口中實現的,只能由實現接口的類來實現接口中的方法。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.5.4 接口的作用","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"使用接口可以繼承超過一個以上的來源,這樣可以爲不同的需求組合出不同的繼承層次。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"接口規範了實現類,必須實現的方法和屬性。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":3},"content":[{"type":"text","text":"1.6 接口與抽象類","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Java中,可以通過兩種形式來體現","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"OOP","attrs":{}}],"attrs":{}},{"type":"text","text":"的抽象:","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"接口","attrs":{}}],"attrs":{}},{"type":"text","text":"和","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"抽象類","attrs":{}}],"attrs":{}},{"type":"text","text":", 它倆有很多相似之處。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.6.1 接口與抽象 區別","attrs":{}}]},{"type":"numberedlist","attrs":{"start":1,"normalizeStart":1},"content":[{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":1,"align":null,"origin":null},"content":[{"type":"text","text":"抽象類可以提供成員方法的實現細節,而接口中只能存在","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public abstract ","attrs":{}}],"attrs":{}},{"type":"text","text":"方法;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":2,"align":null,"origin":null},"content":[{"type":"text","text":"抽象類中的成員變量可以是各種類型的,而接口中的成員變量只能是","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"public static final","attrs":{}}],"attrs":{}},{"type":"text","text":"類型的;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":3,"align":null,"origin":null},"content":[{"type":"text","text":"接口中不能含有靜態代碼塊以及靜態方法,而抽象類可以有靜態代碼塊和靜態方法;","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":4,"align":null,"origin":null},"content":[{"type":"text","text":"一個類只能繼承一個抽象類,而一個類卻可以實現多個接口。","attrs":{}}]}]},{"type":"listitem","attrs":{"listStyle":null},"content":[{"type":"paragraph","attrs":{"indent":0,"number":5,"align":null,"origin":null},"content":[{"type":"codeinline","content":[{"type":"text","text":"interface","attrs":{}}],"attrs":{}},{"type":"text","text":" 接口 是針對事物的 行爲 [ 方法 ] 來進行抽象的, 而 抽象類 是針對事物的共性來 對 行爲 和 屬性 來進行抽象的。","attrs":{}}]}]}]},{"type":"heading","attrs":{"align":null,"level":4},"content":[{"type":"text","text":"1.6.2 例子","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"接口 interface","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public interface PeopleInterface {\n void eat();\n void study();\n void play();\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"抽象類 abstract","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public abstract class People {\n String name = \"人類\";\n abstract void watch();\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","marks":[{"type":"strong","attrs":{}}],"text":"子類","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"codeblock","attrs":{"lang":"java"},"content":[{"type":"text","text":"public class Test extends People implements PeopleInterface {\n public static void main(String[] args) {\n Test test = new Test(); // 子類擁有抽象類的屬性\n System.out.println(test.name);\n test.eat();\n }\n\n @Override\n public void eat() {\n System.out.println(\"喫的功能\");\n }\n\n @Override\n public void study() {\n System.out.println(\"學習功能\");\n }\n\n @Override\n public void play() {\n System.out.println(\"玩的功能\");\n }\n\n @Override\n void watch() {\n System.out.println(\"看的功能\");\n }\n\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"上面例子中講了 類 與 接口 抽象類之間關係。 首先 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":" Test","attrs":{}}],"attrs":{}},{"type":"text","text":" 類 繼承了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"People","attrs":{}}],"attrs":{}},{"type":"text","text":" 抽象類,然後又 實現了 ","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"PeopleInterface","attrs":{}}],"attrs":{}},{"type":"text","text":" 接口。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"子類繼承 抽象類的同時,必須實現抽象類的抽象方法,子類實例化時,擁有抽象類的屬性。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"子類實現 接口的同時, 必須實現接口中所有的接口行爲。","attrs":{}}]},{"type":"heading","attrs":{"align":null,"level":2},"content":[{"type":"text","text":"結語","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"以上是對","attrs":{}},{"type":"codeinline","content":[{"type":"text","text":"Java 面向對象","attrs":{}}],"attrs":{}},{"type":"text","text":" 的總結分享,希望能對你有所啓發,歡迎提出意見!","attrs":{}}]}]}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章