一、構造函數(Constructor)
(1)有別於 common function 的命名規則,constructor 名稱必須與 class 名稱相同;
(2)constructor 沒有返回值(注意區別於 void);
二、函數重載(Method Overloading)
(1)通過 Arguments List 以至於 Arguments 不同的順序(一般不採用這麽 SB 的方法)區分;
(2)primitives 的重載
1)所提供數據的類型小於引數的類型時,該數據的類型會獲得晉昇;
2)與1)反之,若大於,compiler 會報錯;
(3)Return Value 不能作爲重載函數的區分基准;
三、Default Constructor
(1)當 class 不具備 Constructor 時,compiler 會自動合成一個 Default Constructor,如:
// new Bird() 就是 Compiler 在生成 nb 時強制合成的 Default Constructor Bird nb = new Bird();
四、關鍵字 this
(1)this 代表當前對象,並自動生成 Object Reference;
(2)通過 this,在 Constructor 中調用 Constructor,如:
/** ** 1)通過 this 只能調用一個 Constructor; ** 2)調用動作必須置於最起始處; ** 3)Compiler 不允許在 Constructor 以外的函數內調用 Constructor。 **/ Flower(int petals) { System.out.println("Flower(int petals)"); } Flower(String s, int petals) { this(petals); }
(3)static 函數沒有 this(所以可不必經對象來調用);static 函數中無法調用 non-static 函數;
五、Cleanup
(1)Garbage Collection 不等於 Destruction;
(2)Object 有可能永遠都不被回收;
(3)Garbage Collection 只回收由 new 分配的內存;
(4)finalize() 主要用於“對象生成”之外方式分配的存儲空間;
(5)
1)System.gc():強迫性的作用於所有對象;
2)System.runFinalization():作用於未被終結的對象;
3)引數 all:以上兩個。
六、成員初始化
(1)初始化的優先級高於 Constructor;
(2)static 的初始化只會被執行一次;
(3)static/non-static 的明確初始化:
//static 的初始化 static Cup c1; static Cup c2; static { c1 = new Cup(1); c2 = new Cup(2); } //non-static 的初始化 Bra b1; Bra b2; { c1 = new Bra(1); c2 = new Bra(2); }
(4)Array 的初始化
1)不允許直接定義 Array 的大小;
2)Array 可相互賦值(Reference 的複制);
3)Objects Array 是由 Object Reference 而非 Objects 自身組成的;
4)Objects Array 初始化的兩種形式:
(5)Multidimensional Arrays
//多維數組初始化示例(int) int[][] a = { { 1, 2, 3 }, { 4, 5, 6 } }; //多維數組初始化示例(Integer 對象) Integer[][] b = { { new Integer(1), new Integer(2) }, { new Integer(3), new Integer(4) } };
