绗?3 娆¤ Effective Java锛岃繖 58 涓妧宸ф渶鍊硷紒

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了绗?3 娆¤ Effective Java锛岃繖 58 涓妧宸ф渶鍊硷紒相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e6%89%a9%e5%b1%95' title='鎵╁睍'>鎵╁睍   鍙傛暟   瀹氫箟   boolean   鍥炴敹鏈哄埗   鎴愮啛   push   operation   绫诲瀷   

銆奅ffective Java銆婮ava鍚嶈憲锛屽繀璇汇€傚鏋滆兘涓ユ牸閬典粠鏈枃鐨勫師鍒欙紝浠ョ紪鍐橝PI鐨勮川閲忔潵鑻涙眰鑷繁鐨勪唬鐮侊紝浼氬ぇ澶ф彁鍗囩紪鐮佺礌璐ㄣ€?/p>

浠ヤ笅鍐呭鍙褰曚簡鎴戣嚜宸辨暣鐞嗙殑涓滆タ锛岃繕鏄缓璁鍘熸枃銆備负浜嗚仛鐒︾煡璇嗙偣锛屼竴浜涜鏄庢晠鎰忓拷鐣ユ帀浜嗐€傜浉褰撲簬鏄竴绡囨憳瑕併€?/p>

1銆佽€冭檻鐢ㄩ潤鎬佸伐鍘傛柟娉曟浛浠f瀯閫犲嚱鏁?/strong>

渚嬪瓙锛?br> Integer.valueOf(鈥?鈥?銆丅oolean.valueOf(鈥渢rue鈥?绛夈€?/p>

浼樺娍锛?/p>

  • 鍙鎬ч珮锛堟柟娉曞悕锛?/li>
  • 鎬ц兘锛堜笉涓€瀹氬垱寤哄璞★級
  • 鐏垫椿鎬ч珮

涓嬮潰閽堝涓変釜浼樺娍杩涜涓€浜涜В璇汇€?/p>

鍙鎬ч珮
new Point(x,y)鍜孭oint.at(x,y)銆丳oint.origin()銆傛瀯閫犲嚱鏁板彧鑳界湅鍑轰袱涓弬鏁帮紝涓嶇煡鍏舵剰锛屽悗鑰呮洿鏄撶悊瑙c€?/p>

鎬ц兘
鍦ㄦ煇浜涙儏鍐典笅锛屽彲浠ヤ簨鍏堣繘琛屽疄渚嬪寲涓€浜涘璞★紝璋冪敤鏃剁洿鎺ヨ皟鐢ㄥ嵆鍙紝涓嶉渶瑕佽繘琛屾敼鍙樸€傛瘮濡傦紝Boolean銆?/p>

public final class Boolean implements Serializable, Comparable<Boolean> {
    // 棰勫厛璁剧疆涓や釜瀵硅薄
    public static final Boolean TRUE = new Boolean(true);
    public static final Boolean FALSE = new Boolean(false);

    public Boolean(boolean var1) {
        this.value = var1;
    }

    public Boolean(String var1) {
        this(parseBoolean(var1));
    }

    // 宸ュ巶鏂规硶
    public static Boolean valueOf(boolean var0) {
        return var0?TRUE:FALSE; // 杩斿洖棰勫厛璁剧疆鐨勫璞★紝鑰屼笉鏄垱寤哄璞?    }
    // 宸ュ巶鏂规硶
    public static Boolean valueOf(String var0) {
        return parseBoolean(var0)?TRUE:FALSE;
    }
    // ... other code
}

鐏垫椿鎬ч珮
鍙牴鎹叿浣撴儏鍐碉紝杩斿洖瀛愮被銆傜浉褰撲簬鏇村己澶х殑宸ュ巶銆傜洿鎺ヤ粠鐖剁被鑾峰彇鍒板瓙绫汇€傚挨鍏堕€傜敤浜庡伐鍏风被锛堟彁渚涘悇绉岮PI锛夈€備緥瀛愶細Collections銆?/p>

public class Collections {
    // 绉佹湁锛屽吀鍨嬪伐鍘?    private Collections() {
    }

    public static final List EMPTY_LIST = new EmptyList<>();
    // 宸ュ巶鏂规硶
    public static final <T> List<T> emptyList() {
        return (List<T>) EMPTY_LIST;
    }
    private static class EmptyList<E> extends AbstractList<E> implements RandomAccess, Serializable {
    // code
    }

    // 宸ュ巶鏂规硶
    public static <E> List<E> checkedList(List<E> list, Class<E> type) {
    // 鏍规嵁鍏蜂綋鎯呭喌锛岃幏鍙栫浉搴斿瓙绫?        return (list instanceof RandomAccess ?
                new CheckedRandomAccessList<>(list, type) :
                new CheckedList<>(list, type));
    }

    // 瀛愮被1
    static class CheckedRandomAccessList<E> extends CheckedList<E> implements RandomAccess {
        CheckedRandomAccessList(List<E> list, Class<E> type) {
            super(list, type);
        }

        public List<E> subList(int fromIndex, int toIndex) {
            return new CheckedRandomAccessList<>(
                    list.subList(fromIndex, toIndex), type);
        }
    }

    // 瀛愮被2
    static class CheckedList<E> extends CheckedCollection<E> implements List<E> {
    // code
    }
}

2銆佸涓瀯閫犲嚱鏁版椂锛岃€冭檻浣跨敤鏋勯€犲櫒

灏ゅ叾鍦ㄨ繘琛孉ndroid寮€鍙戞椂锛屼細纰板埌杩欑鎯呭喌銆傞€氬父鏄竴涓璞★紝鍏锋湁澶氫釜鎴愬憳鍙橀噺鍙兘闇€瑕佸垵濮嬪寲锛屽父瑙勬柟娉曪紝闇€瑕佹彁渚涘ぇ閲忔瀯閫犲嚱鏁般€備緥濡傦細

// 闈濧ndroid涓殑AlertDialog锛屼究浜庤鏄庨棶棰橈紝涓句釜渚嬪瓙
public class AlertDialog {
    private int width;
    private int height;
    private String title;
    private String confirmText;
    private String denyText;

    private AlertDialog(){}
    public AlertDialog(int width, int height){ // 绌虹櫧鐨勮鍛婃
         AlertDialog(width,height,null);
    }

    // 甯︽爣棰樼殑璀﹀憡妗?    public AlertDialog(int width, int height, String title){ // 甯︽爣棰樼殑璀﹀憡妗?        AlertDialog(width, height, title, "纭畾");
    }

    // 甯︽爣棰樼殑璀﹀憡妗嗭紝鏈夌‘瀹氭寜閽?    public AlertDialog(int width, int height, String title, String confirm){
        AlertDialog(width, height, title, confirm, null);
    }

    // 甯︽爣棰樼殑璀﹀憡妗嗭紝鏈夌‘瀹氭寜閽紝鍙栨秷鎸夐挳
    public AlertDialog(int width, int height, String title, String confirm, String denyText){
        // set every thing.
    }
}

鏈夊绉嶆牱寮忕殑璀﹀憡妗嗭紝涓轰簡璋冪敤鏂逛究锛屽繀椤绘彁渚涘涓瀯閫犲嚱鏁般€傚惁鍒欑敤鎴峰湪璋冪敤鏃讹紝鍙兘浣跨敤瀹屾暣鏋勯€犲嚱鏁帮紝瀹规槗鐘敊涓旀棤娉曡繘琛岄槄璇汇€傛瀬涓嶇伒娲汇€傚鏋滈噰鐢ㄥ彟澶栦竴绉嶆柟寮忥紝鍒欏彲浠ヨВ鍐筹紝浣嗕細鑺辫垂寰堝缁忓巻澶勭悊骞跺彂鐨勬儏鍐?

// 闈濧ndroid涓殑AlertDialog锛屼究浜庤鏄庨棶棰橈紝涓句釜渚嬪瓙
public class AlertDialog {
    private int width;
    private int height;
    private String title;
    private String confirmText;
    private String denyText;

    public AlertDialog(){}// 绌虹櫧鐨勬瀯閫犲嚱鏁?
    public void setWidth(int width){
        this.width = width;
    }
    // 鍏朵粬set鏂规硶
}

璋冪敤鏃讹紝閫氳繃璋冪敤鍚勪釜鍙傛暟鐨剆et鏂规硶杩涜璁剧疆銆傞棶棰樻潵浜嗭細

  • 骞跺彂
  • 鏃犳硶杩涜鍙傛暟鏍¢獙銆?/li>

渚嬪锛屽彧鍒涘缓浜嗗璞★紝璁剧疆浜嗘爣棰橈紝鍗存病鏈夊昂瀵革紝鐩稿綋浜庡垱寤轰簡涓€涓病鏈夊昂瀵哥殑璀﹀憡妗嗐€?/p>

鍦ˋndroid涓紝澶ч噺鐨勬帶浠堕兘浣跨敤浜嗘瀯閫犲櫒Builder銆?/p>

// 闈濧ndroid涓殑AlertDialog锛屼究浜庤鏄庨棶棰橈紝涓句釜渚嬪瓙
public class AlertDialog {
    private int width;
    private int height;
    private String title;
    private String confirmText;
    private String denyText;

    // private
    private AlertDialog(){}

    // Builder涓娇鐢?    protected AlertDialog(Builder b){
        width = b.width;
        height = b.height;
        // .....
        if(width==0||height==0) throws new Exception("size must be set");
    }

    // 鏋勯€犲櫒
    public static class Builder {
        private int width;
        private int height;
        private String title;
        private String confirmText;
        private String denyText;

        // 娉ㄦ剰锛氳繑鍥炵殑Builder銆?        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }
        // 鍏朵粬set...

        public AlertDialog build(){
            return AlertDialog(this);
        }
    }
}

浜庢槸锛屽彲浠ユ牴鎹浉搴旈渶姹傦紝杩涜鐩稿簲璁剧疆锛屽苟鍦ˋlertDialog鐪熸鏋勯€犳椂锛岃繘琛屽弬鏁版牎楠屻€傚氨鍍忚繖鏍凤細

new AlertDialog.Builder().setTitle("鎻愮ず").build();

涓婅堪渚嬪瓙锛屼細鎴愬姛鎶涘嚭寮傚父銆?/p>

3銆佺敤绉佹湁鍖栨瀯閫犲櫒鎴栬€呮灇涓惧瀷寮哄寲Singleton銆?/strong>

Singleton鎸囨渶澶氫細琚疄渚嬪寲涓€娆$殑绫汇€傞€氬父鎯呭喌涓嬶紝浠ュ墠鐨勫仛娉曟槸娌℃湁闂鐨勩€備絾鏄湪鏌愪簺楂樼骇鎯呭喌锛岄€氳繃浣跨敤鍙嶅皠鐨勭浉鍏崇煡璇嗚闂畃rivate鐨勬瀯閫犲嚱鏁帮紝鐮村潖Singleton銆?/p>

public class Elvis{
    // 娉ㄦ剰锛屽叕鏈塮inal瀵硅薄
    public static final Elvis INSTANCE = new Elvis();
    private Elvis(){}
}

鍙︿竴绉嶆儏鍐碉紝鍦ㄥ簭鍒楀寲鐨勮繃绋嬩腑锛屽弽搴忓垪鍖栧緱鍒扮殑瀵硅薄宸茬粡涓嶅啀鏄互鍓嶇殑瀵硅薄锛堢牬鍧忎簡Singleton锛夛紝杩欑鎯呭喌涓嬶紝鍙互閫氳繃鍗曞厓绱犳灇涓惧瀷澶勭悊銆?/p>

public enum Elvis{
    INSTANCE;
    // some methods
}

4銆侀€氳繃绉佹湁鍖栨瀯閫犲櫒寮哄寲涓嶅彲瀹炰緥鍖栫殑鑳藉姏

鏈変竴浜涘伐鍏风被锛屼粎浠呮槸鎻愪緵涓€浜涜兘鍔涳紝鑷繁鏈韩涓嶅叿澶囦换浣曞睘鎬э紝鎵€浠ワ紝涓嶉€傚悎鎻愪緵鏋勯€犲嚱鏁般€傜劧鑰岋紝缂哄け鏋勯€犲嚱鏁扮紪璇戝櫒浼氳嚜鍔ㄦ坊鍔犱笂涓€涓棤鍙傜殑鏋勯€犲櫒銆傛墍浠ワ紝闇€瑕佹彁渚涗竴涓鏈夊寲鐨勬瀯閫犲嚱鏁般€備负浜嗛槻姝㈠湪绫诲唴閮ㄨ鐢紝鍐嶅姞涓婁竴涓繚鎶ゆ帾鏂藉拰娉ㄩ噴銆?/p>

public class Util{
    private Util(){
        // 鎶涘嚭寮傚父锛岄槻姝㈠唴閮ㄨ璋冪敤
        throw new AssertionError();
    }
}

寮婄鏄棤娉曞璇ョ被杩涜缁ф壙锛堝瓙绫讳細璋冪敤super()锛夈€?/p>

5銆侀伩鍏嶅垱寤轰笉蹇呰鐨勫璞?/strong>

  • 瀵硅薄鐨勯噸鐢?/li>
  • 鏄傝吹鐨勫璞★紝浣跨敤瀵硅薄姹?/li>
  • 寤変环鐨勫璞★紝鎱庣敤瀵硅薄姹犮€?/li>

鐜颁唬JVM瀵瑰粔浠峰璞$殑鍒涘缓鍜岄攢姣侀潪甯稿揩锛屾鏃朵笉閫備簬浣跨敤瀵硅薄姹犮€?/p>

6銆佹秷闄よ繃鏈熺殑瀵硅薄寮曠敤

浠ヤ笅涓夌鎯呭喌鍙兘浼氶€犳垚鍐呭瓨娉勯湶锛?/p>

  • 鑷繁绠$悊鐨勫唴瀛橈紙鏁扮粍闀垮害鍑忓皬鍚庯紝pop鍑虹殑瀵硅薄瀹规槗瀵艰嚧鍐呭瓨娉勬紡锛?br> 缂撳瓨
  • 鐩戝惉鍜屽洖璋?/li>
  • 鑷繁绠$悊鐨勫唴瀛?/li>

瀵逛簬鑷繁绠$悊鐨勫唴瀛樿灏忓績锛屾瘮濡傦細

public class Stack{
    private Object[] elements;
    private int size = 0;
    private static final int DEFAULT_INITIAL_CAPACITY = 16;

    public Stack(){
         elements = new Object[DEFAULT_INITIAL_CAPACITY];
    }

    public void push(Object e){
        ensureCapacity();
        elements[size++]=e; // allocate鏂扮殑鍫嗗唴瀛樺拰鏍堝唴瀛?    }

    public Object pop(){
        if(size==0) throw new EmptyStackException();
        return element[--size]; // pop鍑篹lement[size]锛岃瀵硅薄涓嶅啀鏈夋晥銆傚唴瀛樻硠婕忓師鍥犮€?    }

    private void ensureCapacity(){
        if(elements.length==size)
            elements = Arrays.copyOf(elements, 2*size+1);
    }
}

寮瑰嚭鐨勫璞′笉鍐嶆湁鏁堬紝浣咼VM涓嶇煡閬擄紝鎵€浠ヤ細涓€鐩翠繚鎸佽瀵硅薄锛岄€犳垚鍐呭瓨娉勯湶銆?/p>

瑙e喅锛?/p>

public Object pop(){
        if(size==0) throw new EmptyStackException();
        elements[size] = null; // 绛夊緟鍥炴敹
        return element[--size];
    }

缂撳瓨
缂撳瓨鐨勫璞″鏄撹绋嬪簭鍛橀仐蹇橈紝闇€瑕佽缃満鍒舵潵缁存姢缂撳瓨锛屼緥濡備笉瀹氭湡鍥炴敹涓嶅啀浣跨敤鐨勭紦瀛橈紙浣跨敤瀹氭椂鍣級銆傛煇浜涙儏鍐典笅锛屼娇鐢╓eakHashMap鍙互杈惧埌缂撳瓨鍥炴敹鐨勫姛鏁堛€傛敞锛屽彧鏈夌紦瀛樹緷璧栦簬澶栭儴鐜锛岃€屼笉鏄緷璧栦簬鍊兼椂锛學eakHashMap鎵嶆湁鏁堛€?/p>

鐩戝惉鎴栧洖璋?/strong>
浣跨敤鐩戝惉鍜屽洖璋冭璁颁綇鍙栨秷娉ㄥ唽銆傜‘淇濆洖鏀剁殑鏈€濂界殑瀹炵幇鏄娇鐢ㄥ急寮曠敤锛坵eak reference锛夛紝渚嬪锛屽彧灏嗕粬浠繚瀛樻垚WeakHashMap鐨勯敭銆?/p>

7銆侀伩鍏嶆樉绀鸿皟鐢℅C

Java鐨凣C鏈夊己澶х殑鍥炴敹鏈哄埗锛屽彲浠ョ畝鍗曠殑璁颁綇锛氫笉瑕佹樉绀鸿皟鐢╢inalizer銆傚彲浠ヨ繖鏍风悊瑙o細

jvm鏄拡瀵瑰叿浣撶殑纭欢璁捐鐨勶紝鐒惰€岀▼搴忓嵈涓嶆槸閽堝鍏蜂綋纭欢璁捐鐨勶紝鎵€浠ワ紝java浠g爜鏃犳硶寰堝ソ鐨勮В鍐砱c闂锛堝洜涓轰粬鍏锋湁骞冲彴宸紓鍖栵級銆傚彟澶栵紝finalizer鐨勬€ц兘寮€閿€涔熼潪甯稿ぇ锛屼粠杩欎釜瑙掑害涓婅€冭檻涔熶笉搴旇浣跨敤瀹冦€?/p>

8銆佽鐩杄quals鏂规硶璇烽伒瀹堥€氱敤绾﹀畾

  • 鑷弽鎬с€倄.equals(x) == true
  • 瀵圭О鎬с€傚綋鍓嶄粎褰搚.equals(x)true鏃讹紝x.equals(y)true
  • 浼犻€掓€с€俰f(x.equals(y)&&y.equals(z))锛寉.equals(z)==true
    涓€鑷存€с€?/li>
  • 闈炵┖鎬с€倄.equals(null)==false

9銆佽鐩杄quals鏂规硶鏃舵€昏瑕嗙洊hashCode

涓轰簡淇濊瘉鍩轰簬鏁e垪鐨勯泦鍚堜娇鐢ㄨ绫伙紙HashMap銆丠ashSet銆丠ashTable锛夛紝鍚屾椂锛屼篃鏄疧bject.hashCode鐨勯€氱敤绾﹀畾锛岃鐩杄quals鏂规硶鏃讹紝蹇呴』瑕嗙洊hashCode銆?/p>

10銆佸缁堣鐩杢oString

Object鐨則oString鏂规硶鐨勯€氱敤绾﹀畾鏄瀵硅薄鐨勬弿杩般€傛敞鎰忚鐩栨椂锛屽鏋滄湁鏍煎紡锛岃澶囨敞鎴栬€呬弗鏍兼寜鐓ф牸寮忚繑鍥炪€?/p>

11銆佽皑鎱庤鐩朿lone

12銆佽€冭檻瀹炵幇Comparable鎺ュ彛

13銆佷娇绫诲拰鎴愬憳鐨勫彲璁块棶鎬ф渶灏忓寲

鐩殑鏄В鑰︺€傜畝鍗曟潵璁诧紝浣跨敤淇グ绗︾殑浼樺厛绾т粠澶у埌灏忥紝private>protected>default(缂虹渷)>public銆傚鏋滃湪璁捐涔嬪垵锛岃璁′负private淇グ绗﹀悗锛屽湪涔嬪悗鐨勭紪鐮佽繃绋嬪鏋滀笉寰椾笉鎵╁ぇ鍏朵綔鐢ㄤ簬锛屽簲璇ュ厛妫€鏌ユ槸鍚﹁璁$殑纭姝ゃ€?/p>

瀛愮被瑕嗙洊瓒呯被锛屼笉鍏佽璁块棶绾у埆浣庝簬瓒呯被鐨勮闂骇鍒€傦紙瓒呯被鐨刾rotected锛屽瓙绫昏鐩栧悗涓嶈兘鏀逛负default锛夈€?/p>

鎴愬憳鍙橀噺鍐充笉鍏佽鏄叕鏈夌殑銆備竴鏃﹁缃负鍏湁锛屽垯鏀惧純浜嗗浠栧鐞嗙殑鑳藉姏銆傝繖绉嶇被骞朵笉鏄嚎绋嬪畨鍏ㄧ殑銆傚嵆浣挎槸final鐨勶紝涔熶笉鍏佽銆傞櫎闈炲笇鏈涢€氳繃public static final鏉ユ毚闇插父閲忋€傛垚鍛樺彉閲忔€绘槸闇€瑕佷娇鐢╯etter鍜実etter鏉ョ淮鎶ゃ€傛湁涓€涓緥澶栵細闀垮害闈為浂鐨勬暟缁勩€傝繖鏄畨鍏ㄦ紡娲炵殑涓€涓牴婧愩€?/p>

public Object pop(){
        if(size==0) throw new EmptyStackException();
        elements[size] = null; // 绛夊緟鍥炴敹
        return element[--size];
    }

鏀硅繘锛?/p>

private static final Thing[] PRIVATE_VALUES = {...}
// 姝ゆ椂鑾峰彇鍒扮殑鎵嶆槸鈥滃父閲忊€?public static final List<Thing> VALUS =
    Collections.unmodifiableList(Arrays.asList(PRIVATE_VALUES))

鍙︿竴绉嶏細

private static final Thing[] PRIVATE_VALUES = {...}
// 姝ゆ椂鑾峰彇鍒扮殑鎵嶆槸鈥滃父閲忊€?public static final Thing[] values(){
    return PRIVATE_VALUES.clone();
}

14銆佸湪鍏湁绫讳腑浣跨敤璁块棶鏂规硶鑰岄潪鍏湁鎴愬憳鍙橀噺锛堢被浼?3锛?/strong>

15銆佷娇鍙彉鎬ф渶灏忓寲

16銆佸鍚堜紭鍏堜簬缁ф壙

缁ф壙鏈夊埄浜庝唬鐮佸鐢紝浣嗘槸灏藉彲鑳戒笉瑕佽繘琛岃法鍖呯殑缁ф壙銆傚寘鍐呯殑缁ф壙鏄紭绉€鐨勮璁℃柟寮忥紝涓€涓寘閲岀殑鏂囦欢澶勫湪鍚屼竴涓▼搴忓憳鐨勬帶鍒朵箣涓嬨€備絾鏄户鎵挎湁鍏跺眬闄愭€э細瀛愮被渚濊禆浜庤秴绫汇€傝秴绫讳竴鏃﹀彂鐢熸洿鏀癸紝灏嗗彲鑳界牬鍧忓瓙绫汇€傚苟涓旓紝濡傛灉瓒呯被鏄湁缂洪櫡鐨勶紝瀛愮被涔熶細寰椻€滈仐浼犵梾鈥濄€?/p>

澶嶅悎锛屽嵆涓嶆墿灞曞凡鏈夌殑绫伙紝鑰屾槸鍦ㄧ殑绫讳腑鏂板涓€涓幇鏈夌被鐨勩€傜浉褰撲簬鐜版湁绫讳綔涓轰竴涓粍寤哄瓨鍦ㄤ簬鏂扮被涓€傚姝わ紝灏嗗彧浼氱敤鍒伴渶瑕佺敤鍒扮殑涓滆タ锛岃€屼笉琛ㄧ幇鐜版湁绫绘墍鏈夌殑鏂规硶鍜屾垚鍛樺彉閲忋€傛柊绫讳篃鍙互绉颁负鈥滃寘瑁呯被鈥濓紝涔熷氨鏄璁℃ā寮忎腑鐨凞ecorate妯″紡銆?/p>

17銆佽涔堝氨涓虹户鎵胯€岃璁★紝骞舵彁渚涙枃妗h鏄庯紝瑕佷箞灏辩姝㈢户鎵?/strong>

18銆佹帴鍙d紭浜庢娊璞$被

19銆佹帴鍙e彧鐢ㄤ簬瀹氫箟绫诲瀷

20銆佺被灞傛浼樺厛浜庢爣绛剧被

21銆佺敤鍑芥暟瀵硅薄琛ㄧず绛栫暐

鍑芥暟鍙傛暟鍙互浼犲叆绫讳技listener鐨勫璞★紝鐩殑鏄娇鐢╨istener涓殑鏂规硶銆傚鏋滀娇鐢ㄥ尶鍚嶇殑鍙傛暟锛屾瘡涓€娆¤皟鐢ㄤ細鍒涘缓鏂扮殑瀵硅薄銆傚彲浠ュ皢listener澹版槑涓烘垚鍛樺彉閲忥紝姣忔閮藉鐢ㄥ悓涓€涓璞★紝骞朵笖鍙互浣跨敤闈欐€佸煙锛坰tatic鍙橀噺锛夈€傛瘮濡係tring绫荤殑CASE_INSENSITIVE_ORDER鍩熴€?/p>

22銆佷紭鍏堣€冭檻闈欐€佺被鎴愬憳

宓屽绫荤殑鐩殑搴旇鍙槸涓轰簡浠栫殑澶栧洿绫绘彁渚涙湇鍔★紝濡傛灉浠ュ悗杩樺彲鑳界敤浜庡叾浠栫幆澧冧腑锛屽垯搴旇璁捐涓洪《灞傜被銆傞潤鎬佺被鐩稿綋浜庝竴涓櫘閫氱殑澶栭儴绫伙紝鍙槸鎭板ソ澹版槑鍦ㄤ簡涓€涓被鍐呴儴銆傞€氬父鐨勭敤鎴锋槸锛欳alculator.Operation.PLUS绛夈€傚拰鏅€氱被鐨勫尯鍒彧鏄紝鍦≒LUS鍓嶏紝鏈変簡2涓墠缂€锛屾潵琛ㄦ槑鍏跺惈涔夈€傝€岄潪闈欐€佺被蹇呴』瀛樺湪浜庡閮ㄧ被瀵硅薄涓€備笉瑕佹墜鍔ㄥ湪澶栭儴鍒涘缓涓€涓唴閮ㄩ潪闈欐€佺被瀵硅薄锛屽垱寤虹殑杩囩▼鏄細instance.New MemberClass()銆傝繖闈炲父濂囨€€?/p>

濡傛灉鎴愬憳绫讳笉闇€瑕佽闂鍥寸被锛屽垯闇€瑕佹坊鍔爏tatic锛屾槸浠栨垚涓洪潤鎬佹垚鍛樼被锛屽惁鍒欐瘡涓疄渚嬮兘灏嗗寘鍚竴涓澶栨寚鍚戝鍥村璞$殑寮曠敤銆傚皢浼氬奖鍝嶅瀮鍦惧洖鏀舵満鍒躲€?/p>

23銆佸簲鎸囧畾娉涘瀷鐨勫叿浣撶被鍨嬶紝鑰屼笉鏄洿鎺ヤ娇鐢ㄥ師鐢熺被鍨嬨€?/strong>

渚嬪锛屽簲璇ユ寚瀹歀ist锛岃€屼笉寤鸿鐩存帴浣跨敤List銆?/p>

24銆佹秷闄ら潪棣栨璀﹀憡

鍦ㄤ娇鐢↖DE杩涜缂栫爜鏃讹紝寮哄ぇ鐨処DE閮戒細鍦ㄤ綘缂栫爜杩囩▼涓彁绀簑arning锛岄渶瑕佸敖鍙兘鐨勬秷闄arning锛岃嚦灏戯紝搴旇灏忓績杩欎簺warning銆傛厧鐢⊿uppresWarning锛屽鏋淚DE鎻愮ず浣犲彲浠ラ€氳繃娣诲姞璇ユ敞瑙hВ鍐虫帀warning锛岃涓嶈閭d箞鍋氥€傚鏋滃疄鍦ㄨ浣跨敤锛岃娣诲姞娉ㄩ噴璇存槑鍘熷洜銆?/p>

25銆佸垪琛ㄤ紭鍏堜簬鏁扮粍

绫绘瘮娉涘瀷锛屾暟缁勬槸鏈変竴瀹氱己闄风殑銆侺ist鍜孡ist鏄病鏈夊叧绯荤殑锛岃€孲ub[]鏄疭uper[]鐨勫瓙绫汇€?/p>

// Fails at runtime
Object[] objectArray = new Long[1];
objectArray[0] = "I don鈥榯 fit in"; // throw exception

// won鈥榯 compile
List<Object> ol = new ArrayList<Long>(); // Incompatible types
ol.add("I don鈥榯 fit in");

浠庝唬鐮佷腑鍙互鐪嬪埌锛屼娇鐢ㄦ硾鍨嬶紝浼氭彁鍓嶅彂鐜伴敊璇€?/p>

26銆佷紭鍏堣€冭檻娉涘瀷

27銆佷紭鍏堣€冭檻娉涘瀷鏂规硶

28銆佸埄鐢ㄦ湁闄愬埗閫氶厤绗︽潵鎻愬崌API鐨勭伒娲绘€?/strong>

PECS锛宲roducer-extends锛宑onsumer-super銆?/p>

//public class Stack<E>{
// public Stack();
// public void push(E e);
// public E pop();
// public boolean isEmpty();
//}

public void pushAll(Iterator<? extends E> src){
    for(E e : src)
        push(e);
}

public void popAll(Collection<? super E> dst){
    while(!isEmpty()){
        dst.add(pop());
    }
}

// Get and Put Principle

鎵€鏈塩omparable鍜宑omparator閮芥槸娑堣垂鑰咃紙Consumer锛夈€?/p>

29銆佷紭鍏堣€冭檻绫诲瀷瀹夊叏鐨勫紓鏋勫鍣?/strong>

30銆佺敤enum浠f浛int甯搁噺

public enum Apple { FUJI, PIPPIN, GRANNY_SMITH }
public enum Orange { NAVEL, TEMPLE, BLOOD }

鏋氫妇鍨嬪湪java涓潪甯稿己澶э紝褰撻渶瑕佷竴缁勫浐瀹氬父閲忔椂锛屼娇鐢╡num姣攊nt濂藉緢澶氥€傛瘮濡備唬鐮佸彲璇绘€э紝瀹夊叏鎬х瓑銆?/p>

31銆乪num鐢ㄥ疄渚嬪煙浠f浛搴忔暟

// bad solution
public enum Ensemble {
    SOLO, DUET, TRIO, QUARTET, QUINTET,
    SEXTET, SEPTET, OCTET, NONET, DECTET;

    public int numberOfMusicians() { return ordinal() + 1; }
}
//

// improvement
public enum Ensemble {
    SOLO(1), DUET(2), TRIO(3), QUARTET(4), QUINTET(5),
    SEXTET(6), SEPTET(7), OCTET(8), NONET(9), DECTET(10), TRIPLE_QUARTET(12);

    private final int numberOfMusicians;
    Ensemble(int size) { this.numberOfMusicians = size; }
    public int numberOfMusicians() { return numberOfMusicians; }
}

姘歌繙涓嶈鍍忕涓€绉嶇殑鏂瑰紡锛屽埄鐢ㄥ簭鏁拌闂甧num锛岄渶瑕佸湪鏋勯€犲嚱鏁颁腑浣跨敤鍙傛暟鏉ュ垵濮嬪寲銆?/p>

32銆佺敤EnumSet浠f浛浣嶅煙

public class Text{
    public static final int STYLE_BOLD = 1 << 0; // 1
    public static final int STYLE_ITALIC = 1 << 1; // 2
    public static final int STYLE_UNDERLINE = 1 << 2; // 4
    public static final int STYLE_STRIKETHROUGH = 1 << 3; // 8

    public void applyStyles(int styles){
        // ...
    }
}

//
text.applyStyles(STYLE_BOLD | STYLE_ITALIC);

浠ヤ笂鍙仛浣嶅浘娉曪紝浣嗘槸鏈夋洿濂界殑鏂规鏉ヤ紶閫掑缁勫父閲忊€斺€擡numSet銆?/p>

public class Text{
    public enum Style { BOLD, ITALIC, UNDERLINE, STRIKETHROUGH }

    // 娉ㄦ剰姝ゅ锛屼娇鐢ㄧ殑鏄疭et鑰屼笉鏄疎numSet
    public void applyStyles(Set<Style> styles){
        // ...
    }
}

//
text.applyStyles(EnumSet.of(Style.BOLD, Style.ITALIC));

33銆佺敤EnumMap浠f浛搴忔暟绱㈠紩

浠讳綍鏃跺€欓兘涓嶈浣跨敤enum鐨刼rdinal()鏂规硶銆?/p>

34銆佺敤鎺ュ彛妯℃嫙鍙几缂╃殑鏋氫妇

35銆佹敞瑙d紭鍏堜簬鍛藉悕妯″紡

36銆佸潥鎸佷娇鐢∣verride娉ㄨВ

38銆佹鏌ュ弬鏁扮殑鏈夋晥鎬?/strong>

鍏湁鏂规硶妫€鏌ュ弬鏁帮紝鍙傛暟寮傚父闇€瑕佽窇鍑篍xception銆傜鏈夋柟娉曞埄鐢ㄦ柇瑷€assertion妫€鏌ュ弬鏁般€?/p>

39銆佸繀瑕佹椂杩涜淇濇姢鎬ф嫹璐?/strong>

鍋囪绫荤殑瀹㈡埛绔細灏藉叾鎵€鑳芥潵鐮村潖杩欎釜绫荤殑绾︽潫鏉′欢锛屽洜姝や綘蹇呴』淇濇姢鎬х殑璁捐绋嬪簭銆備互涓嬫槸涓€涓笉鍙彉绫荤殑璁捐銆?/p>

public Period(Date start, Date end){
    this.start = new Date(start); // 浣跨敤浜嗗€肩殑鎷疯礉锛屾病鏈変娇鐢ㄥ師瀵硅薄锛堟寚閽堬級
    this.end = new Date(end);
    if(this.start.compareTo(this.end)>0)
        throw new IllegalArgumentException(start + " after " + end)
}

娉ㄦ剰锛氫繚鎶ゆ€ф嫹璐濇槸鍦ㄦ鏌ュ弬鏁颁箣鍓嶈繘琛岀殑锛岄槻姝㈠绾跨▼鐨勫奖鍝嶃€備笉瑕佷娇鐢╟lone鏂规硶杩涜淇濇姢鎬ф嫹璐濄€?/p>

浠ヤ笂鏂规硶闃插尽浜嗕紶鍏ュ弬鏁扮殑淇敼锛屼絾鏄浜巊et鏂规硶鑾峰彇鍒扮殑瀵硅薄锛屼粛鐒跺彲浠ヨ淇敼锛岄€氳繃浠ヤ笅鏂规硶鍙互闃叉杩欑鏀诲嚮銆?/p>

public Date start(){
    return new Date(start);
}

public Date end(){
    return new Date(end);
}

40銆佽皑鎱庤璁℃柟娉曠鍚?/strong>

41銆佹厧鐢ㄩ噸杞?/strong>

42銆佹厧鐢ㄥ彲鍙樺弬鏁?/strong>

43銆佽繑鍥?闀垮害鐨勬暟缁勬垨鑰呴泦鍚堬紝鑰屼笉鏄痭ull

null涓€鑸敤浜庤〃绀烘病鏈夎鍒濆鍖栨垨澶勭悊锛屽鏋滄柟娉曡繑鍥炰簡null锛屽垯闇€瑕佸湪涓婂眰鍋氭洿澶氱殑澶勭悊锛屼互闃叉NPE銆?/p>

44銆佷负鎵€鏈夊鍑虹殑API鍏冪礌缂栧啓鏂囨。娉ㄩ噴

姝g‘鐨刯avadoc鏂囨。锛岄渶瑕佹瘡涓瀵煎嚭鐨勭被銆佹帴鍙c€佹瀯閫犲櫒銆佹柟娉曞拰鍩熶箣鍓嶅鍔犳枃妗f敞閲娿€傛敞閲婂簲璇ユ槸瀵瑰疄鐜伴€忔槑鐨勶紝鍙渶瑕佺畝娲佺殑鎻忚堪瀹冨拰瀹㈡埛绔箣闂寸殑绾﹀畾銆傚苟涓旓紝杩樺簲璇ラ檮涓婅鏂规硶鐨勫壇浣滅敤銆?/p>

45銆佸皢灞€閮ㄥ彉閲忕殑浣滅敤鍩熸渶灏忓寲

46銆乫or-each浼樺厛浜巉or寰幆

for-each瑙勯伩鎺変簡for寰幆鐨刬ndex鍙橀噺鐨勫紩鐢紝閫氬父鏉ヨ瀹冩槸涓嶅繀瑕佺殑鈥斺€斾細澧炲姞寮曞叆閿欒鐨勯闄╋紝骞朵笖椋庨櫓涓€鏃﹀彂鐢燂紝寰堥毦琚彂鐜般€備笉杩囨湁涓夌鎯呭喌涓嬶紝鏃犳硶浣跨敤for-each锛堟敞锛氬湪jdk1.8涓凡缁忓緢濂界殑瑙e喅浜嗚繖浜涢棶棰橈級銆?/p>

  • 杩囨护
  • 杞崲
  • 骞宠杩唬

47銆佸鏋滈渶瑕佺簿纭殑绛旀锛岃閬垮厤浣跨敤float鍜宒ouble

float鍜宒ouble鏄墽琛岀殑浜岃繘鍒舵诞鐐硅繍绠楋紝鐩殑鏄湪骞挎硾鏁板€艰寖鍥翠笂浣跨敤绮剧‘鐨勫揩閫熻繎浼艰绠楄€岃璁$殑銆傜劧鑰屼粬浠苟娌℃湁鎻愪緵瀹屽叏绮剧‘鐨勮绠楋紙瀹為檯搴旂敤涓紝缁忓父浼氱鍒板嚭鐜皒.99999绛夌粨鏋滐級銆傚挨鍏舵槸锛屽湪杩涜璐у竵璁$畻鏃讹紝浠栦滑骞朵笉閫傜敤銆傛瘮濡傦細

System.out.println(1.03-.42);

寰楀埌鐨勭粨鏋滃皢鏄細0.610000000001銆?/p>

涓轰簡瑙e喅杩欎釜闂锛岄渶瑕佷娇鐢˙igDecimal銆傜劧鑰岃繖涔熸湁涓€浜涢棶棰橈紝鐩稿浜庢櫘閫氱殑杩愮畻锛屽畠鏄惧緱鏇村姞楹荤儲锛岃€屼笖涔熸洿鎱€傞€氬父鏉ヨ鍚庝竴涓己鐐瑰彲浠ュ拷鐣ワ紝浣嗘槸鍓嶈€呭彲鑳戒細璁╀汉寰堜笉鑸掓湇銆傛湁涓€绉嶅仛娉曟槸灏嗛渶瑕佸鐞嗙殑鏁板€?10(鎴栨洿澶氾級锛屼娇鐢╥nt杩涜璁$畻锛屼笉杩囬渶瑕佷綘鑷繁澶勭悊鍥涜垗浜斿叆绛夋搷浣溿€?/p>

48銆佸熀鏈被鍨嬩紭鍏堜簬瑁呯鍩烘湰绫诲瀷

鍩烘湰绫诲瀷鍙湁鍊硷紝瑁呯绫诲叿鏈変笌浠栦滑鍊间笉鍚岀殑鍚屼竴鎬с€?/p>

鍩烘湰绫诲瀷鍙湁鍔熻兘瀹屽鐨勫€硷紝瑁呯绫昏繕鍏锋湁闈炲姛鑳藉€硷細

null銆?/p>

鎵€浠ヤ綘鍙兘浼氱鍒癗PE

鍩烘湰绫诲瀷鐪佺┖闂寸渷鏃堕棿

50銆佸鏋滄湁鏇寸簿纭殑绫诲瀷锛岃閬垮厤浣跨敤瀛楃涓?/strong>

瀛楃涓蹭笉閫傚悎浠f浛鍏朵粬鍊肩殑绫诲瀷銆?br> 渚嬪锛歩nt锛宐oolean绛?/p>

  • 涓嶉€傚悎浠f浛鏋氫妇绫诲瀷锛堢30鏉★級
  • 涓嶉€傚悎鑱氶泦绫诲瀷

50銆佸綋蹇冨瓧绗︿覆杩炴帴鐨勬€ц兘

鎿嶄綔绗︹€?鈥濆彲浠ュ皢澶氫釜瀛楃涓茶繘琛岃繛鎺ャ€備絾鏄湪澶ц妯′娇鐢ㄢ€?鈥濈殑鎯呭喌涓嬶紝杩炴帴n涓瓧绗︿覆鐨勫紑閿€鏄痭鐨勫钩鎴跨骇鏃堕棿銆傝繖鏄敱浜庡瓧绗︿覆鐨勪笉鍙彉鎬у鑷寸殑銆傚湪杩欑鎯呭喌涓嬭浣跨敤StringBuilder杩涜杩炴帴銆?/p>

51銆侀€氳繃鎺ュ彛寮曠敤瀵硅薄

52銆佹帴鍙d紭鍏堜簬鍙嶅皠鏈哄埗

浣跨敤鍙嶅皠鏈哄埗浼氬甫鏉ヤ互涓嬬殑闂锛?/p>

  • 涓уけ浜嗙紪璇戞湡绫诲瀷妫€鏌?/li>
  • 浠g爜绗ㄦ嫏鍐楅暱
  • 鎬ц兘鎹熷け

鍙嶅皠鍩烘湰涓婂彧閫傚悎鐢ㄥ湪缂栧啓缁勪欢鏃躲€佷唬鐮佸垎鏋愬櫒銆丷PC绛夊満鏅笅浣跨敤銆傚湪浣跨敤鍙嶅皠鏈哄埗鏃讹紝濡傛灉鍙兘锛屽敖鍙兘鍙€氳繃鍙嶅皠鏈哄埗瀹炰緥鍖栧璞★紝鑰岃闂柟娉曟椂锛屼娇鐢ㄥ凡鐭ョ殑鎺ュ彛鎴栬€呰秴绫汇€?/p>

53銆佽皑鎱庝娇鐢↗NI

54銆佽皑鎱庤繘琛屼紭鍖?/strong>

寰堝璁$畻涓婄殑杩囧け閮借褰掑拵浜庢晥鐜囷紙娌℃湁蹇呰杈惧埌鐨勬晥鐜囷級锛岃€屼笉鏄换浣曞叾浠栧師鍥犫€斺€旂敋鑷冲寘鎷洸鐩殑鍋氬偦浜嬨€?/p>

鈥斺€擶illiam A. Wulf

涓嶈鍘昏杈冩晥鐜囦笂鐨勪竴浜涘皬灏忕殑寰楀け锛屽湪97%鐨勬儏鍐典笅锛屼笉鎴愮啛鐨勪紭鍖栨墠鏄竴鍒囬棶棰樼殑鏍规簮銆?/p>

鈥斺€擠onald E. Knuth

鍦ㄤ紭鍖栨柟闈紝鎴戜滑搴旇閬靛畧涓ゆ潯瑙勫垯锛?/p>

瑙勫垯1:涓嶈杩涜浼樺寲銆?/p>

瑙勫垯2锛堜粎閽堝涓撳锛夛細杩樻槸涓嶈杩涜浼樺寲鈥斺€斾篃灏辨槸璇达紝鍦ㄤ綘杩樻病鏈夌粷瀵规竻鏅扮殑浼樺寲鏂规鍓嶏紝璇蜂笉瑕佽繘琛屼紭鍖栥€?/p>

鈥斺€擬. A. Jackson

杩欎簺鏍艰█姣攋ava鐨勫嚭鐜拌繕瑕佹棭20骞淬€備粬浠杩颁簡涓€涓叧浜庝紭鍖栫殑娣卞埢浜嬪疄锛氫紭鍖栫殑寮婂ぇ浜庡埄銆?/p>

瑕佸姫鍔涚紪鍐欏ソ鐨勭▼搴忥紝鑰屼笉鏄揩鐨勭▼搴忋€備綆鑰﹀悎鐨勯噸瑕佹€ц繙杩滃ぇ浜庢€ц兘銆傚綋绋嬪簭缂栧啓寰楄冻澶熶綆鑰﹀悎鍚庯紝閫氳繃宸ュ叿鍙戠幇浜嗘€ц兘鐡堕鐨勪唬鐮佸潡锛屾墠鍙互淇濊瘉瀵瑰叾鐨勪慨鏀逛笉褰卞搷浠讳綍澶栭儴鐜銆?/p>

55銆侀伒瀹堟櫘閬嶇殑鍛藉悕瑙勫垯

56銆佸彧閽堝寮傚父鎯呭喌鎵嶄娇鐢ㄥ紓甯?/strong>

涓嶈灏濊瘯閫氳繃寮傚父鏈哄埗鏉ュ仛姝e父浠g爜搴旇鍋氱殑浜嬫儏锛屾瘮濡傦紝妫€鏌ユ暟缁勪笅鏍囥€?/p>

jvm寰堝皯瀵瑰紓甯歌繘琛屼紭鍖栵紝鍥犱负瀹冨彧鐢ㄤ簬涓嶆甯哥殑鎯呭喌銆傚苟涓旓紝濡傛灉浣犲皢浠g爜鏀惧叆try-catch浠g爜鍧楋紝jvm灏变抚澶变簡鏈潵鍙互瀵瑰畠杩涜鐨勪紭鍖栥€?/p>

57銆佸浜庡彲鎭㈠鐨勬儏鍐典娇鐢ㄥ彈妫€寮傚父锛屽浜庣紪绋嬮敊璇殑鎯呭喌浣跨敤杩愯鏃跺紓甯?/strong>

濡傛灉鏈熸湜璋冪敤鑰呴€傚綋鐨勬仮澶嶏紝鍒欓渶瑕佷娇鐢ㄥ彈妫€寮傚父锛屽己杩皟鐢ㄨ€呴鐢╰ry-catch浠g爜鍧楋紝鎴栬€呭皢浠栦滑鎶涘嚭鍘?/p>

褰撹皟鐢ㄥ彂鐢熷墠鎻愯繚渚嬧€斺€旇繚鍙嶇害瀹氱殑鎯呭喌鏃讹紝浣跨敤杩愯鏃跺紓甯革紝杩欎釜鏃跺€欑▼搴忓凡缁忔棤娉曞啀鎵ц涓嬪幓浜嗐€?/p>

渚嬪璋冪敤鏁扮粍鐨?1绱㈠紩銆?/p>

浣滆€咃細Dong GuoChao







以上是关于绗?3 娆¤ Effective Java锛岃繖 58 涓妧宸ф渶鍊硷紒的主要内容,如果未能解决你的问题,请参考以下文章

Eclipse閲岄€変腑涓€涓彉閲忓悗锛岃繖涓被閲岀殑璇ュ彉閲忎笉鍙樿壊浜嗭紵

鏂伴矞鍑虹倝锛岃繖鏄叏缃戣鐨勬渶璇︾粏鐨剆pringboot鏁村悎娑堟伅鏈嶅姟浜嗗惂锛屽缓璁敹钘忥紒

銆愯禒绁?鎶樻墸鐮併€戠‘璁よ繃鐪肩锛屼綘鏄垜瑕佺瓑鐨勫墠绔汉锛?019绗?灞婁腑鍥藉墠绔紑鍙戣€呭ぇ浼氭潵鍟︼紒

闃舵涓€-02.鍒嗙被锛屾帹鑽愶紝鎼滅储锛岃瘎浠凤紝璐墿杞﹀紑鍙?绗?绔?鍟嗗搧鎺ㄨ崘+鎼滅储鍔熻兘瀹炵幇-2-1 鍟嗗搧鎺ㄨ崘 - 闇€姹傚垎鏋愪笌sql鏌ヨ

瀹夊崜鎵嬫満鐢ㄤ笂iPhone绯荤粺锛佺偒閰凤紒