Java基礎學習 四 (垃圾回收、枚舉類型、訪問權限控制)

垃圾回收

  finalize的用法  (從object類中繼承得到)

    垃圾回收器回收對象的時候先調用這個類的finalize方法。此方法在object中實現。當使用其他語言許釋放內存時可在此方法中寫語句進行內存回收。java可自動回收內存。爲了減小資源的消耗java回收機制當垃圾達到一定程度時纔會自動調用垃圾回收機制釋放內存。

    finalize 必須是一個無返回值類型protected修飾符修飾的方法 即必須爲 protected void finalize(){}

    System.gc(); 的作用是啓動垃圾回收機制

public class Test {
    public static void main(String[] args) {
        ClassA a = new ClassA("A");
        new ClassA("B");
        Test t = new Test();
        t.method();
        System.gc();
    }
    void method(){
        ClassA c = new ClassA("C");
    }
}
class ClassA{
    private String name;
    ClassA(String name){
        this.name = name;
    }
    @Override
    protected void finalize(){
        System.out.println( this.name+"類將被垃圾回收機制處理");
    }
}

     

    爲什麼只有B對象和C對象被回收了呢 A的對象不也沒用了麼 爲什麼沒有回收?

    B對象沒有引用 所以被垃圾回收機制回收  C對象在t.method()方法執行完後C對象生命週期結束,C對象沒有引用能都指向它所以被垃圾回收機制處理

    A對象的生命週期爲整個main方法 當main方法執行完後A的引用才失去作用 ,所以在使用垃圾回收機制處理時A對象的引用還存在,不會被處理。

 

垃圾回收器如何工作   參考

    我從這個文章中截取了一段圖片放這裏看全部文章點擊參考

枚舉類型  默認繼承 java.lang.Enum 根據java單繼承機制 枚舉類不能繼承其他類 但是能實現其他接口

  這篇文章可做參考 重點看原理分析

  創建一個簡單的枚舉類型

//把 class 替換成 enum 關鍵字
public enum SimpleEnum {
    // 枚舉值
    BIG,MEDIUM,SMALL;
}

使用枚舉

public class Main {

    public static void main(String[] args) {
        SimpleEnum a = SimpleEnum.SMALL;
        System.out.println(a);            ------+
        System.out.println(SimpleEnum.SMALL); --+  兩個其實是一樣的
        System.out.println(SimpleEnum.SMALL.ordinal());  //枚舉實例的位置 也就是0開始第幾個
    }
}

  結果   可以看出一個枚舉值就相當於這個枚舉的對象

  

  給枚舉綁定信息( 使用枚舉把邏輯代碼裏面不應該出現常量字符串和常量數字之類的東西替換成定義的常量)

  這裏就需要給枚舉實例綁定對應的信息

  

public enum SimpleEnum {
    /**
     * 大的
     */
    // 把code 在初始化時 傳給構造方法
    BIG(100),
    /**
     * 中等的
     */
    MEDIUM(50),
    /**
     * 小的
     */
    SMALL(1);

    // 枚舉實例綁定的code
    private Integer code;

    // 枚舉的構造方法可使 初始化時對常量進行綁定
    SimpleEnum(Integer code){
        this.code = code;
    }
    
    // 添加get構造方法 可獲取枚舉實例對應的code
    public Integer getCode() {
        return code;
    }
}

  用法

public class Main {

    public static void main(String[] args) {
        Integer a = SimpleEnum.SMALL.getCode();
        System.out.println(a);
        System.out.println(SimpleEnum.SMALL.getCode());
        System.out.println(SimpleEnum.MEDIUM.getCode());
        System.out.println(SimpleEnum.BIG.getCode());
    }
}

  結果  可以看到可直接使用枚舉類.枚舉實例.getCode() 獲取對應的code

  

  

  如何讓一個code對應 一個message呢 

    

public enum SimpleEnum {
    /**
     * 大的
     */
    BIG(100,"大"),
    /**
     * 中等的
     */
    MEDIUM(50,"中等"),
    /**
     * 小的
     */
    SMALL(1,"小");

    private Integer code;
    private String message;

    SimpleEnum(Integer code,String message){
        this.code = code;
        this.message = message;
    }


    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }
}

  使用

public class Main {

    public static void main(String[] args) {
        Integer code = SimpleEnum.SMALL.getCode();
        String message = SimpleEnum.SMALL.getMessage();
        System.out.println(code +":"+message);
        System.out.println(SimpleEnum.SMALL.getCode()+":"+SimpleEnum.SMALL.getMessage());
        System.out.println(SimpleEnum.MEDIUM.getCode()+":"+SimpleEnum.MEDIUM.getMessage());
        System.out.println(SimpleEnum.BIG.getCode()+":"+SimpleEnum.BIG.getMessage());
    }
}

  結果

  

  如何 通過id獲取message呢  又如何通過code獲取對應的枚舉實例呢

public enum SimpleEnum {
    /**
     * 大的
     */
    BIG(100, "大"),
    /**
     * 中等的
     */
    MEDIUM(50, "中等"),
    /**
     * 小的
     */
    SMALL(1, "小");

    private Integer code;
    private String message;

    SimpleEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 通過code獲取對應的枚舉實例
     *
     * @param code code
     * @return 枚舉實例
     */
    public static SimpleEnum getEnumByCode(Integer code) {
        for (SimpleEnum simpleEnum : SimpleEnum.values()) {
            if (code != null && code.equals(simpleEnum.code)) {
                return simpleEnum;
            }
        }
        return null;
    }

    /**
     * 通過code 獲取對應的 message
     *
     * @param code code
     * @return message
     */
    public static String getMessageByCode(Integer code) {
        return getEnumByCode(code) != null ? getEnumByCode(code).message : null;
    }
}

  使用

public class Main {

    public static void main(String[] args) {
        //獲取code對應的枚舉實例
        System.out.println(SimpleEnum.getEnumByCode(100));
        //獲取code對應的message信息
        System.out.println(SimpleEnum.getMessageByCode(100));
    }
}

  結果

  

  枚舉類型可以當成一個class 不過這個class 比較特殊 

  枚舉可以跟類一樣可以寫在單獨創建 入上面 一個枚舉一個類文件  也可以跟類寫在一個類文件中(如下面的Enum1)  可以寫在類內(入下面的Enum2)

  

  枚舉用於switch 

public class Main {
    public static void main(String[] args) {
        Enum1 big = Enum1.BIG;
        switch (big) {
            case BIG:
                System.out.println("大的");
                break;
            case SMALL:
                System.out.println("小的");
                break;
            case MEDIUM:
                System.out.println("中等的");
                break;
            default:
                System.out.println("沒找到");
        }
    }
}

enum Enum1 {
    /**
     * 大的
     */
    BIG,
    /**
     * 中等的
     */
    MEDIUM,
    /**
     * 小的
     */
    SMALL
}

  結果

  

訪問權限控制

  包的權限   同一個包下不需要導入

    

    如圖 SimpleEnum類在mypackage包下 那麼在Main類中就無法訪問 如果想訪問需要在類名前加上具體報名

    

    或者 使用import

    

  權限修飾符

  類訪問權限

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章