相關最新代碼已上傳至個人GitHub了(https://github.com/WenyangSun/ThinkingInJava),後續例子沒有在博客上更新。git
package com.ietree.base.initialization; // 在類的內部,變量定義的前後順序決定了初始化的順序。即便變量定義散佈於方法定義之間,它們仍舊會在任何方法(包括構造器)被調用以前獲得初始化。 class Window { Window(int maker) { System.out.println("Window(" + maker + ")"); } } class House { House() { System.out.println("House()"); // 4 w3 = new Window(33); // 5 } Window w1 = new Window(1); // 1 Window w2 = new Window(2); // 2 void f() { System.out.println("f()"); // 7 Window w4 = new Window(4); // 8 } Window w3 = new Window(3); // 3 } // output: // Window(1) // Window(2) // Window(3) // House() // Window(33) // f() // Window(4) public class OrderOfInitialization { public static void main(String[] args) { House h = new House(); h.f(); // 6 } }
package com.ietree.base.staticintialization; /** * 初始化的順序是先靜態對象,然後是「非靜態」對象,構造器能夠當作是靜態方法。 */ class Bowl { public Bowl(int marker) { System.out.println("Bowl(" + marker + ")"); } void f1(int marker) { System.out.println("f1(" + marker + ")"); } } class Table { static Bowl bowl1 = new Bowl(1); //2 public Table() { System.out.println("Table()"); //4 bowl2.f1(1); //5 } void f2(int marker) { System.out.println("f2(" + marker + ")"); } static Bowl bowl2 = new Bowl(2); //3 } class Cupboard { Bowl bowl3 = new Bowl(3); //9 //14 //18 static Bowl bowl4 = new Bowl(4); //7 public Cupboard() { System.out.println("Cupboard()");//10 //15 //19 bowl4.f1(2);//11 //16 //20 } void f3(int marker) { System.out.println("f3(" + marker + ")"); } static Bowl bowl5 = new Bowl(5); //8 } //output: //Bowl(1) //Bowl(2) //Table() //f1(1) //Bowl(4) //Bowl(5) //Bowl(3) //Cupboard() //f1(2) //Creating new Cupboard() in main //Bowl(3) //Cupboard() //f1(2) //Creating new Cupboard() in main //Bowl(3) //Cupboard() //f1(2) //f2(1) //f3(1) public class StaticIntialization { public static void main(String[] args) { System.out.println("Creating new Cupboard() in main");//12 new Cupboard();//13 System.out.println("Creating new Cupboard() in main"); new Cupboard(); //17 table.f2(1); //21 cupboard.f3(1);//22 } static Table table = new Table(); //1 static Cupboard cupboard = new Cupboard(); //6 }
package com.ietree.base.reuseclass.extendskey; class Art { Art() { System.out.println("Art constructor"); // 2 } } class Drawing extends Art { Drawing() { System.out.println("Drawing constructor"); // 3 } } // output: // Art constructor // Drawing constructor // Cartoon constructor public class Cartoon extends Drawing { public Cartoon() { System.out.println("Cartoon constructor"); // 4 } public static void main(String[] args) { @SuppressWarnings("unused") Cartoon x = new Cartoon(); // 1 } }
package com.ietree.base.reuseclass.hide; class Homer { char doh(char c) { System.out.println("doh(char)"); return 'd'; } float doh(float f) { System.out.println("doh(float)"); return 1.0f; } } class Milhouse { } class Bart extends Homer { void doh(Milhouse m) { System.out.println("doh(Milhouse)"); } } public class Hide { public static void main(String[] args) { Bart b = new Bart(); b.doh(1); b.doh('x'); b.doh(new Milhouse()); } } // output: // doh(float) // doh(char) // doh(Milhouse)