thinkinginjava

第5章 初始化与清理

2019-04-07  本文已影响0人  胡笠

总结:
- 只有在对象被创建或者第一次访问静态数据的时候,才初始化静态对象
- 初始化非静态对象

5.1 用构造器确保初始化

package main.chapter5;

/**
 * page77
 *
 * @author
 * @create 2019-04-07 下午7:39
 **/
public class Rock {

    private String name;
    private String str1 = "11111111";
    private String str2 = "22222222";
    private String str3 = str2;

    public Rock() {
        System.out.println("this is no params");
    }


    public Rock(String name){
        this.name = name;
        this.str2 = "new2222";
    }




    public static void main(String[] args){

//        构造方法快捷键:command+N
//        sout快捷键

        //question 1:创建一个类,它包含一个未初始化的String引用,验证该引用被Java初始化为null
        Rock A = new Rock();
        System.out.println(A.name);

        //question 2:创建一个类,它包含一个在定义时就被初始化的String域,以及另外一个通过构造器初始化的String域
//        这两种方式有何差异?
        Rock B = new Rock("test");


        System.out.println(B.str1);
        System.out.println(B.str2);
        System.out.println(B.str3);

        //输出结果为
//        11111111
//        new2222
//        22222222
//        .class文件如下
//        public class Rock {
//            private String name;
//            private String str1 = "11111111";
//            private String str2 = "22222222";
//            private String str3;
//
//            public Rock() {
//                this.str3 = this.str2;
//                System.out.println("this is no params");
//            }
//
//            public Rock(String var1) {
//                this.str3 = this.str2;
//                this.name = var1;
//                this.str2 = "new2222";
//            }
//
//            public static void main(String[] var0) {
//                Rock var1 = new Rock();
//                System.out.println(var1.name);
//                Rock var2 = new Rock("test");
//                System.out.println(var2.str1);
//                System.out.println(var2.str2);
//                System.out.println(var2.str3);
//            }
//        }
    }
}

5.5终结条件

finalize(),作为对象终结条件验证,么见过使用

package main.chapter5;

/**
 * 5.5.3终结条件
 *
 * @author
 * @create 2019-04-07 下午9:14
 **/
public class Book {

    boolean ifCheckout = false;

    public Book(boolean ifCheckout) {
        this.ifCheckout = ifCheckout;
    }

    public Book() {
    }

    void checkIn(){
        ifCheckout = false;
    }


    @Override
    protected void finalize() throws Throwable {
        super.finalize();
        if(ifCheckout){
            System.out.println("error,no checkout");
        }
    }

    public static void main(String[] args){
        //从gc root能找到,不会gc
//        虚拟机栈中的引用对象
//        方法区中类静态属性引用的对象
//        方法区中常量引用对象
//        本地方法栈中JNI引用对象
        Book book = new Book(true);

        //找不到,会gc
        new Book(true);
        new Book(true);
        new Book(false);
        //执行soup
        System.gc();
    }
}

5.7构造器初始化

package main.chapter5;

/**
 * 静态数据初始化
 *
 * @author
 * @create 2019-04-07 下午11:53
 **/
public class Bowl {

    public Bowl(int marker) {
        System.out.println("bowl(" + marker + ")");
    }
    void f1(int marker){
        System.out.println("f1(" + marker + ")");
    }
}
package main.chapter5;

/**
 * 静态数据初始化
 *
 * @author
 * @create 2019-04-07 下午11:59
 **/
public class Cupboard {
    Bowl bowl3 = new Bowl(3);
    static Bowl bowl4 = new Bowl(4);

    public Cupboard() {
        System.out.println("Cupboard()");
        bowl4.f1(2);
    }

    void f3(int marker){
        System.out.println("f3(" + marker + ")");
    }

    static Bowl bowl5 = new Bowl(5);
}
package main.chapter5;

/**
 * 静态数据初始化
 *
 * @author
 * @create 2019-04-07 下午11:56
 **/
public class Table {

    static Bowl bowl1 = new Bowl(1);

    public Table() {
        System.out.println("Table()");
        bowl2.f1(1);
    }

    void f2(int marker) {
        System.out.println("f2(" + marker + ")");
    }

    static Bowl bowl2 = new Bowl(2);
}

执行

package main.chapter5;

/**
 * 静态数据初始化
 *
 * @author
 * @create 2019-04-08 上午12:02
 **/
public class StaticInitialization {

    public static void main(String[] args){
        System.out.println("crateing new cupboard() in main");
        new Cupboard();
        System.out.println("creating new cupboard() in main agine");
        new Cupboard();
        table.f2(1);
        cupboard.f3(1);
    }

    static Table table = new Table();
    static Cupboard cupboard = new Cupboard();
}

/**
 * bowl(1)
 * bowl(2)
 * table()
 * f1(1):bowl1(2)
 * bowl4
 * bowl5
 * bowl3
 * cuboard()
 * f1(2)
 * creating...
 * bowl3
 * cuboard()
 * f1(2)
 * creating...
 * bowl3
 * cuboard()
 * f1(2)
 * f2(1)
 * f3(1)
 */

5.8数组初始化

        //练习16-18,page101
        String[] strings = {"1","2","3","4"};
        for (String s :
                strings) {
            System.out.println(s);

        }

        String[] strings1 = new String[]{"4","3","2","1"};
        for (String s :
                strings1) {
            System.out.println(s);
        }

        //只有引用,未执行构造函数        
        Bowl[] bowls = new Bowl[4];
        for (Bowl bowl :
                bowls) {
            System.out.println(bowl);
            //初始化为null
//            null
//            null
//            null
//            null
//        Bowl[] bowls1 = new Bowl[]{new Bowl(1),new Bowl(2),new Bowl(3),new Bowl(4)};

5.8.1可变参数列表

可以传入数组、0个参数

    void flex(Object... args){
        for (Object obj :
                args) {
            System.out.println(obj + " ");
        }
    }
上一篇 下一篇

猜你喜欢

热点阅读