第四章 初始化和垃圾回收 用构造方法完成初始化 当一个方法名和类名相同的时候,它就成了一个构造器,这样一来,这个方法就可以在类被初始化的时候自动调用。如下是一个
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Rock { Rock() { System.out.print("Rock " ); } } public class SimpleConstructor { public static void main (String[] args) { for (int i = 0 ; i < 10 ; i++) new Rock(); } }
构造器没有返回值
重载 当一个类里面有两个名字相同参数不同的方法,就称为重载。区分不同方法的方式就是通过观察传入的参数。
主数据类型的重载 如果传入的数据类型小于参数所期望的变量,编译器就会将传入的数据自动转换为参数的变量。如果传入的数据类型大于参数中变量的范围,则需要手动做转换,例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 public class Demotion { void f1 (char x) { print("f1(char)" ); } void f1 (byte x) { print("f1(byte)" ); } void f1 (short x) { print("f1(short)" ); } void f1 (int x) { print("f1(int)" ); } void f1 (long x) { print("f1(long)" ); } void f1 (float x) { print("f1(float)" ); } void f1 (double x) { print("f1(double)" ); } void f2 (char x) { print("f2(char)" ); } void f2 (byte x) { print("f2(byte)" ); } void f2 (short x) { print("f2(short)" ); } void f2 (int x) { print("f2(int)" ); } void f2 (long x) { print("f2(long)" ); } void f2 (float x) { print("f2(float)" ); } void f3 (char x) { print("f3(char)" ); } void f3 (byte x) { print("f3(byte)" ); } void f3 (short x) { print("f3(short)" ); } void f3 (int x) { print("f3(int)" ); } void f3 (long x) { print("f3(long)" ); } void f4 (char x) { print("f4(char)" ); } void f4 (byte x) { print("f4(byte)" ); } void f4 (short x) { print("f4(short)" ); } void f4 (int x) { print("f4(int)" ); } void f5 (char x) { print("f5(char)" ); } void f5 (byte x) { print("f5(byte)" ); } void f5 (short x) { print("f5(short)" ); } void f6 (char x) { print("f6(char)" ); } void f6 (byte x) { print("f6(byte)" ); } void f7 (char x) { print("f7(char)" ); } void testDouble () { double x = 0 ; print("double argument:" ); f1(x);f2((float )x);f3((long )x);f4((int )x); f5((short )x);f6((byte )x);f7((char )x); } public static void main (String[] args) { Demotion p = new Demotion(); p.testDouble(); } }
返回值方式的重载 不仅可以通过参数不同的方式重载,返回值不同也可以重载
1 2 void f () {} int f () { return 1 ; }
默认构造方法 如果你没有创建构造方法,那么编译器会自动创建一个构造方法
this
关键字传递句柄 使用this关键字可以在当前对象中传递句柄(references)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 public class Leaf { int i = 0 ; Leaf increment () { i++; return this ; } void print () { System.out.println("i = " + i); } public static void main (String[] args) { Leaf x = new Leaf(); x.increment().increment().increment().print(); } }
传递对象 this关键字同样能够将当前的对象传递到别的方法中,例如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 class Person { public void eat (Apple apple) { Apple peeled = apple.getPeeled(); System.out.println("Yummy" ); } } class Peeler { static Apple peel (Apple apple) { return apple; } } class Apple { Apple getPeeled () { return Peeler.peel(this ); } } public class PassingThis { public static void main (String[] args) { new Person().eat(new Apple()); } }
从构造器中调用另外一个构造器 当这个类中有多个构造方法时,可以使用this关键字简化代码如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 import static net.mindview.util.Print.*; public class Flower { int petalCount = 0 ; String s = "initial value" ; Flower(int petals) { petalCount = petals; print("Constructor w/ int arg only, petalCount= " + petalCount); } Flower(String ss) { print("Constructor w/ String arg only, s = " + ss); s = ss; } Flower(String s, int petals) { this (petals); this .s = s; print("String & int args" ); } Flower() { this ("hi" , 47 ); print("default constructor (no args)" ); } void printPetalCount () { print("petalCount = " + petalCount + " s = " + s); } public static void main (String[] args) { Flower x = new Flower(); x.printPetalCount(); } }
static
的含义static方法就是没有this的方法
清除和垃圾收集