Chapter 5 初始化与清理

来源:互联网 发布:熟悉linux内核 编辑:程序博客网 时间:2024/05/23 02:06

this 关键字

 

1)this 关键字只能在方法内部使用,表示对“调用方法的那个对象”的引用。

 

2)可以用this 调用一个构造器,但不能用相同的方法调用两个构造器。例如:

    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); // Can't call two!        this.s = s; // Another use of "this"        print("String & int args");    }


除构造器之外,编译器禁止在其他任何方法中调用构造器。例如:

    void printPetalCount() {        // ! this(11); // Not inside non-constructor!        print("petalCount = " + petalCount + " s = " + s);    }

 


构造器初始化 

 

1)在类的内部,变量定义的先后顺序决定了初始化的顺序。

 

2)static关键字不能应用于局部变量,它只能作用于域。

 

3)初始化的顺序是先“静态”,(如果它们尚未因前面的对象创建过程而被初始化),后“非静态”。

 

4)对象的创建过程会很有帮助。假设有个名为 Dog 的类:


1. 当首次创建类型为 Dog 的对象时(构造器可以看成静态方法),或者Dog 类的静态方法/静态域首次被访问时,Java 解释器必须查找类路径,以定位Dog.class 文件。


2. 然后载入 Dog.class(这将创建一个Class 对象),有关静态初始化的动作都会执行。因此,静态初始化只在Class 对象首次加载的时候进行一次。


3. 当用 new Dog( )创建对象的时候,首先将在堆上为Dog 对象分配足够的存储空间。


4. 这块存储空间会被清零,这就自动地将Dog 中的所有基本类型数据设置成了默认值(对数字来说就是0,对布尔型和字符型也相同),而引用则被设置成了null。


5. 执行所有出现于域定义处的初始化动作。


6. 执行构造器。这可能会牵涉到很多动作,尤其是涉及继承的时候。

 

数组初始化

 

1)编译器不允许你指定数组的大小。

 

2)数组的三种初始化方法,以及注意事项。

/** * 数组的三种初始化方式,数组在定义时不可以指定数组的大小 * */class ArrayTest {/** * 第一种,在定义时进行初始化。 这种形式的初始化只可以在定义处使用。 * */Integer[] integer0 = { 1, 2, 3 };/** * 第二种,在new对象时初始化。 这种形式的初始化不可以指定数组的大小 * */Integer[] integer1 = new Integer[] { 1, 2, 3 };/** * 第三种,利用循环逐一初始化。 这种形式的初始化必须指定数组的大小。 在这种方式下,如果数组存储的数据类型是基本数据类型, * 在数组通过new完成初始化后,所有的数组元素被初始化为该类型的默认值; 如果数组存储的是对象,则所有的数组元素被初始化为null。 * */Integer[] integer2 = new Integer[3];{for (int i = 0; i < integer2.length; i++) {integer2[i] = i;}}}


3)数组和可变参数列表

对于参数个数和参数类型 未知的场合,可以使用数组的第二种初始化方法来产生一个可变参数列表,例如:

class A {}public class VarArgs {static void printArray(Object[] args) {for (Object obj : args)System.out.print(obj + " ");System.out.println();}public static void main(String[] args) {printArray(new Object[] { new Integer(47), new Float(3.14),new Double(11.11) });printArray(new Object[] { "one", "two", "three" });printArray(new Object[] { new A(), new A(), new A() });}} 


而在java5中,可以像下面这样定义可变参数列表:

class A {}public class NewVarArgs {static void printArray(Object... args) {for (Object obj : args)System.out.print(obj + " ");System.out.println();}public static void main(String[] args) {// Can take individual elements:printArray(new Integer(47), new Float(3.14), new Double(11.11));printArray(47, 3.14F, 11.11);printArray("one", "two", "three");printArray(new A(), new A(), new A());// Or an array:printArray(new Integer[] { 1, 2, 3, 4 });printArray(); // Empty list is OK}}

有了可变参数列表,你就不用显示地编写 数组语法了,当你指定参数时,编译器会为你填充数组,你获取的仍旧是一个数组。注意程序的倒数第二行,可变参数列表也可以接受一个数组作为参数。

 

可变参数列表使得重载过程变得复杂:

public class OverloadingVarargs {    static void f(Character... args) {        System.out.print("first");        for (Character c : args)            System.out.print(" " + c);        System.out.println();    }    static void f(Integer... args) {        System.out.print("second");        for (Integer i : args)            System.out.print(" " + i);        System.out.println();    }    static void f(Long... args) {        System.out.println("third");    }    public static void main(String[] args) {        f('a', 'b', 'c');        f(1);        f(2, 1);        f(0);        f(0L);        // ! f(); // Won't compile -- ambiguous    }} 


在不使用参数调用f()时,编译器无法知道应该调用哪个方法。你可能会通过在某个方法中添加一个非可变参数来解决该问题:

public class OverloadingVarargs2 {    static void f(float i, Character... args) {        System.out.println("first");    }    static void f(Character... args) {        System.out.print("second");    }    public static void main(String[] args) {        f(1, 'a');//         f('a', 'b'); //Won't compile -- ambiguous      }}


如果你给这两个方法都添加一个非可变参数,就可以解决问题了:

public class OverloadingVarargs3 {    static void f(float i, Character... args) {        System.out.println("first");    }    static void f(char c, Character... args) {        System.out.println("second");    }    public static void main(String[] args) {        f(1, 'a');        f('a', 'b');    }}


你应该总是只在重载方法的一个版本上使用可变参数列表,或者压根就不使用它。