绗?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> 7銆侀伩鍏嶆樉绀鸿皟鐢℅C Java鐨凣C鏈夊己澶х殑鍥炴敹鏈哄埗锛屽彲浠ョ畝鍗曠殑璁颁綇锛氫笉瑕佹樉绀鸿皟鐢╢inalizer銆傚彲浠ヨ繖鏍风悊瑙o細 jvm鏄拡瀵瑰叿浣撶殑纭欢璁捐鐨勶紝鐒惰€岀▼搴忓嵈涓嶆槸閽堝鍏蜂綋纭欢璁捐鐨勶紝鎵€浠ワ紝java浠g爜鏃犳硶寰堝ソ鐨勮В鍐砱c闂锛堝洜涓轰粬鍏锋湁骞冲彴宸紓鍖栵級銆傚彟澶栵紝finalizer鐨勬€ц兘寮€閿€涔熼潪甯稿ぇ锛屼粠杩欎釜瑙掑害涓婅€冭檻涔熶笉搴旇浣跨敤瀹冦€?/p>
8銆佽鐩杄quals鏂规硶璇烽伒瀹堥€氱敤绾﹀畾 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>
鏀硅繘锛?/p>
鍙︿竴绉嶏細 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>
浠庝唬鐮佷腑鍙互鐪嬪埌锛屼娇鐢ㄦ硾鍨嬶紝浼氭彁鍓嶅彂鐜伴敊璇€?/p>
26銆佷紭鍏堣€冭檻娉涘瀷 27銆佷紭鍏堣€冭檻娉涘瀷鏂规硶 28銆佸埄鐢ㄦ湁闄愬埗閫氶厤绗︽潵鎻愬崌API鐨勭伒娲绘€?/strong> PECS锛宲roducer-extends锛宑onsumer-super銆?/p>
鎵€鏈塩omparable鍜宑omparator閮芥槸娑堣垂鑰咃紙Consumer锛夈€?/p>
29銆佷紭鍏堣€冭檻绫诲瀷瀹夊叏鐨勫紓鏋勫鍣?/strong> 30銆佺敤enum浠f浛int甯搁噺 鏋氫妇鍨嬪湪java涓潪甯稿己澶э紝褰撻渶瑕佷竴缁勫浐瀹氬父閲忔椂锛屼娇鐢╡num姣攊nt濂藉緢澶氥€傛瘮濡備唬鐮佸彲璇绘€э紝瀹夊叏鎬х瓑銆?/p>
31銆乪num鐢ㄥ疄渚嬪煙浠f浛搴忔暟 姘歌繙涓嶈鍍忕涓€绉嶇殑鏂瑰紡锛屽埄鐢ㄥ簭鏁拌闂甧num锛岄渶瑕佸湪鏋勯€犲嚱鏁颁腑浣跨敤鍙傛暟鏉ュ垵濮嬪寲銆?/p>
32銆佺敤EnumSet浠f浛浣嶅煙 浠ヤ笂鍙仛浣嶅浘娉曪紝浣嗘槸鏈夋洿濂界殑鏂规鏉ヤ紶閫掑缁勫父閲忊€斺€擡numSet銆?/p>
33銆佺敤EnumMap浠f浛搴忔暟绱㈠紩 浠讳綍鏃跺€欓兘涓嶈浣跨敤enum鐨刼rdinal()鏂规硶銆?/p>
34銆佺敤鎺ュ彛妯℃嫙鍙几缂╃殑鏋氫妇 35銆佹敞瑙d紭鍏堜簬鍛藉悕妯″紡 36銆佸潥鎸佷娇鐢∣verride娉ㄨВ 38銆佹鏌ュ弬鏁扮殑鏈夋晥鎬?/strong> 鍏湁鏂规硶妫€鏌ュ弬鏁帮紝鍙傛暟寮傚父闇€瑕佽窇鍑篍xception銆傜鏈夋柟娉曞埄鐢ㄦ柇瑷€assertion妫€鏌ュ弬鏁般€?/p>
39銆佸繀瑕佹椂杩涜淇濇姢鎬ф嫹璐?/strong> 鍋囪绫荤殑瀹㈡埛绔細灏藉叾鎵€鑳芥潵鐮村潖杩欎釜绫荤殑绾︽潫鏉′欢锛屽洜姝や綘蹇呴』淇濇姢鎬х殑璁捐绋嬪簭銆備互涓嬫槸涓€涓笉鍙彉绫荤殑璁捐銆?/p>
娉ㄦ剰锛氫繚鎶ゆ€ф嫹璐濇槸鍦ㄦ鏌ュ弬鏁颁箣鍓嶈繘琛岀殑锛岄槻姝㈠绾跨▼鐨勫奖鍝嶃€備笉瑕佷娇鐢╟lone鏂规硶杩涜淇濇姢鎬ф嫹璐濄€?/p>
浠ヤ笂鏂规硶闃插尽浜嗕紶鍏ュ弬鏁扮殑淇敼锛屼絾鏄浜巊et鏂规硶鑾峰彇鍒扮殑瀵硅薄锛屼粛鐒跺彲浠ヨ淇敼锛岄€氳繃浠ヤ笅鏂规硶鍙互闃叉杩欑鏀诲嚮銆?/p>
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绛夌粨鏋滐級銆傚挨鍏舵槸锛屽湪杩涜璐у竵璁$畻鏃讹紝浠栦滑骞朵笉閫傜敤銆傛瘮濡傦細 寰楀埌鐨勭粨鏋滃皢鏄細0.610000000001銆?/p>
涓轰簡瑙e喅杩欎釜闂锛岄渶瑕佷娇鐢˙igDecimal銆傜劧鑰岃繖涔熸湁涓€浜涢棶棰橈紝鐩稿浜庢櫘閫氱殑杩愮畻锛屽畠鏄惧緱鏇村姞楹荤儲锛岃€屼笖涔熸洿鎱€傞€氬父鏉ヨ鍚庝竴涓己鐐瑰彲浠ュ拷鐣ワ紝浣嗘槸鍓嶈€呭彲鑳戒細璁╀汉寰堜笉鑸掓湇銆傛湁涓€绉嶅仛娉曟槸灏嗛渶瑕佸鐞嗙殑鏁板€?10(鎴栨洿澶氾級锛屼娇鐢╥nt杩涜璁$畻锛屼笉杩囬渶瑕佷綘鑷繁澶勭悊鍥涜垗浜斿叆绛夋搷浣溿€?/p>
48銆佸熀鏈被鍨嬩紭鍏堜簬瑁呯鍩烘湰绫诲瀷 鍩烘湰绫诲瀷鍙湁鍊硷紝瑁呯绫诲叿鏈変笌浠栦滑鍊间笉鍚岀殑鍚屼竴鎬с€?/p>
鍩烘湰绫诲瀷鍙湁鍔熻兘瀹屽鐨勫€硷紝瑁呯绫昏繕鍏锋湁闈炲姛鑳藉€硷細 null銆?/p>
鎵€浠ヤ綘鍙兘浼氱鍒癗PE 鍩烘湰绫诲瀷鐪佺┖闂寸渷鏃堕棿 50銆佸鏋滄湁鏇寸簿纭殑绫诲瀷锛岃閬垮厤浣跨敤瀛楃涓?/strong> 瀛楃涓蹭笉閫傚悎浠f浛鍏朵粬鍊肩殑绫诲瀷銆?br>
渚嬪锛歩nt锛宐oolean绛?/p>
50銆佸綋蹇冨瓧绗︿覆杩炴帴鐨勬€ц兘 鎿嶄綔绗︹€?鈥濆彲浠ュ皢澶氫釜瀛楃涓茶繘琛岃繛鎺ャ€備絾鏄湪澶ц妯′娇鐢ㄢ€?鈥濈殑鎯呭喌涓嬶紝杩炴帴n涓瓧绗︿覆鐨勫紑閿€鏄痭鐨勫钩鎴跨骇鏃堕棿銆傝繖鏄敱浜庡瓧绗︿覆鐨勪笉鍙彉鎬у鑷寸殑銆傚湪杩欑鎯呭喌涓嬭浣跨敤StringBuilder杩涜杩炴帴銆?/p>
51銆侀€氳繃鎺ュ彛寮曠敤瀵硅薄 52銆佹帴鍙d紭鍏堜簬鍙嶅皠鏈哄埗 浣跨敤鍙嶅皠鏈哄埗浼氬甫鏉ヤ互涓嬬殑闂锛?/p>
鍙嶅皠鍩烘湰涓婂彧閫傚悎鐢ㄥ湪缂栧啓缁勪欢鏃躲€佷唬鐮佸垎鏋愬櫒銆丷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
浣跨敤鐩戝惉鍜屽洖璋冭璁颁綇鍙栨秷娉ㄥ唽銆傜‘淇濆洖鏀剁殑鏈€濂界殑瀹炵幇鏄娇鐢ㄥ急寮曠敤锛坵eak reference锛夛紝渚嬪锛屽彧灏嗕粬浠繚瀛樻垚WeakHashMap鐨勯敭銆?/p>
涓€鑷存€с€?/li>
public Object pop(){
if(size==0) throw new EmptyStackException();
elements[size] = null; // 绛夊緟鍥炴敹
return element[--size];
}
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();
}
// 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");
//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
public enum Apple { FUJI, PIPPIN, GRANNY_SMITH }
public enum Orange { NAVEL, TEMPLE, BLOOD }
// 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; }
}
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);
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));
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)
}
public Date start(){
return new Date(start);
}
public Date end(){
return new Date(end);
}
System.out.println(1.03-.42);
以上是关于绗?3 娆¤ Effective Java锛岃繖 58 涓妧宸ф渶鍊硷紒的主要内容,如果未能解决你的问题,请参考以下文章
Eclipse閲岄€変腑涓€涓彉閲忓悗锛岃繖涓被閲岀殑璇ュ彉閲忎笉鍙樿壊浜嗭紵
鏂伴矞鍑虹倝锛岃繖鏄叏缃戣鐨勬渶璇︾粏鐨剆pringboot鏁村悎娑堟伅鏈嶅姟浜嗗惂锛屽缓璁敹钘忥紒
銆愯禒绁?鎶樻墸鐮併€戠‘璁よ繃鐪肩锛屼綘鏄垜瑕佺瓑鐨勫墠绔汉锛?019绗?灞婁腑鍥藉墠绔紑鍙戣€呭ぇ浼氭潵鍟︼紒
闃舵涓€-02.鍒嗙被锛屾帹鑽愶紝鎼滅储锛岃瘎浠凤紝璐墿杞﹀紑鍙?绗?绔?鍟嗗搧鎺ㄨ崘+鎼滅储鍔熻兘瀹炵幇-2-1 鍟嗗搧鎺ㄨ崘 - 闇€姹傚垎鏋愪笌sql鏌ヨ