Java編程思想【溫故知新】

來源:互聯網
上載者:User

標籤:pre   單根繼承   其他   虛擬   建立對象   hashmap   return   next   ==   

第一章:對象導論

1. 抽象過程(類與對象的關係)

  類是一類對象的共同行為(成員函數)與狀態(成員變數),對象是具體類的執行個體化。(Eg.人類是一個類,共同的行為:吃,狀態:名字。)

  【類建立者需要考慮這件事情,回頭看看這個概念四個字醍醐灌頂,每次建立這個類的時候,想一想這個類是需要什麼成員函數與成員變數來滿足單一職責的原則】

2. 每個對象都提供服務:程式設計本身的目標就是去建立能夠提供服務來解決問題的一系列對象。

3. 被隱藏的具體實現:類建立者與用戶端程式員使用者。

  往往來說,每個程式員都是兩者身份的結合,類的建立者只要需要暴露需要暴露的行為,用戶端程式員使用者負責建立對象並使用。【封裝】

4. 複用具體類實現:在建立新的類時,應該優先考慮組合。Has a

  【開放封閉原則】

5. 繼承:

  以現有類為基礎,複製它,在子類中添加新的方法或者覆蓋舊的方法。is a(子類完全可以替換父類)與is like a(子類擁有父類不具備的功能)【多態】

6. 伴隨多態的可互換對象

  向上轉型是安全的【裡氏替換原則】,向下轉型不是安全的。【依賴倒轉原則】

7. 單根繼承結構Object

  任何一個類都是繼承至Object。

8. 容器(問題來了,容器是否會持有對其他對象的引用?答案:都是引用,會修改未經處理資料。抽查得出結果。)

  Set:

    HashSet:

    public boolean add(E e) {        return map.put(e, PRESENT)==null;    }    public V put(K key, V value) {        if (table == EMPTY_TABLE) {            inflateTable(threshold);        }        if (key == null)            return putForNullKey(value);        int hash = sun.misc.Hashing.singleWordWangJenkinsHash(key);        int i = indexFor(hash, table.length);        for (HashMapEntry<K,V> e = table[i]; e != null; e = e.next) {            Object k;            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {                V oldValue = e.value;                e.value = value;                e.recordAccess(this);                return oldValue;            }        }        modCount++;        addEntry(hash, key, value, i);        return null;    }

  LinkedHashSet:extends HashSet。

  TreeSet:

  List:

    ArrayList:

    public boolean add(E e) {        ensureCapacityInternal(size + 1);  // Increments modCount!!        elementData[size++] = e;        return true;    }

    LinkedList:

    public boolean add(E e) {        linkLast(e);        return true;    }    void linkLast(E e) {        final Node<E> l = last;        final Node<E> newNode = new Node<>(l, e, null);        last = newNode;        if (l == null)            first = newNode;        else            l.next = newNode;        size++;        modCount++;    }

  Map:

    HashMap:

    public V put(K key, V value) {        if (table == EMPTY_TABLE) {            inflateTable(threshold);        }        if (key == null)            return putForNullKey(value);        int hash = sun.misc.Hashing.singleWordWangJenkinsHash(key);        int i = indexFor(hash, table.length);        for (HashMapEntry<K,V> e = table[i]; e != null; e = e.next) {            Object k;            if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {                V oldValue = e.value;                e.value = value;                e.recordAccess(this);                return oldValue;            }        }        modCount++;        addEntry(hash, key, value, i);        return null;    }    void addEntry(int hash, K key, V value, int bucketIndex) {        if ((size >= threshold) && (null != table[bucketIndex])) {            resize(2 * table.length);            hash = (null != key) ? sun.misc.Hashing.singleWordWangJenkinsHash(key) : 0;            bucketIndex = indexFor(hash, table.length);        }        createEntry(hash, key, value, bucketIndex);    }    void createEntry(int hash, K key, V value, int bucketIndex) {        HashMapEntry<K,V> e = table[bucketIndex];        table[bucketIndex] = new HashMapEntry<>(hash, key, value, e);        size++;    }    HashMapEntry(int h, K k, V v, HashMapEntry<K,V> n) {            value = v;            next = n;            key = k;            hash = h;    }    

  另外:其實List就是將Map的key變成下標:0/1/2/3.../n,而Set就是將Map的value變成null。

9. 對象的建立與生命週期

  雖然說java有記憶體回收機制,但是前提是必須瞭解java記憶體模型(pc、虛擬機器棧、本地方法棧、堆、方法區各自的作用)

10. 異常處理:處理錯誤

11. 並發編程

  1、區分並發與並行的區別,假設只有一個處理器,兩者個人認為是表現一致的。

  2、並發與並行的區別:在於同一時刻。(Eg. 並發:你在吃飯的過程中,電話來了,你只能放下手中的飯筷去聽電話。並行:你在吃飯的過程中,電話來了,你可以邊吃飯邊聽電話。)

  3、在java編程中,程式是不關心並行或者是並發,只有邏輯線程的概念。

 

Java編程思想【溫故知新】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.