文章詳情頁
Java程序的編碼規(guī)范(3)
瀏覽:131日期:2024-07-03 09:26:15
內容: Java程序的編碼規(guī)范(3)作者:李小敏 本文選自:IBM DW中國 2002年08月21日 ● 構造函數(shù) 接下來是構造函數(shù),它應該用遞增的方式寫(比如:參數(shù)多的寫在后面)。 訪問類型 ('public', 'private' 等.) 和 任何 'static', 'final' 或 'synchronized' 應該在一行中,并且方法和參數(shù)另寫一行,這樣可以使方法和參數(shù)更易讀。 publicCounterSet(int size){ this.size = size;} ● 克隆方法 如果這個類是可以被克隆的,那么下一步就是 clone 方法: publicObject clone() { try { CounterSet obj = (CounterSet)super.clone(); obj.packets = (int[])packets.clone(); obj.size = size; return obj; }catch(CloneNotSupportedException e) { throw new InternalError('Unexpected CloneNotSUpportedException: ' + e.getMessage()); }} ● 類方法 下面開始寫類的方法: /** * Set the packet counters * (such as when restoring from a database) */protected finalvoid setArray(int[] r1, int[] r2, int[] r3, int[] r4) throws IllegalArgumentException{ // // Ensure the arrays are of equal size // if (r1.length != r2.length || r1.length != r3.length || r1.length != r4.length) throw new IllegalArgumentException('Arrays must be of the same size'); System.arraycopy(r1, 0, r3, 0, r1.length); System.arraycopy(r2, 0, r4, 0, r1.length);} ● toString 方法 無論如何,每一個類都應該定義 toString 方法: publicString toString() { String retval = 'CounterSet: '; for (int i = 0; i
標簽:
Java
相關文章:
1. IntelliJ IDEA 2020如何設置背景圖片的方法步驟2. 關于Intellij idea 報錯:Error : java 不支持發(fā)行版本5的問題3. 用php+mysql一個名片庫程序4. 確保php應用程序的安全-不能違反的四條安全規(guī)則5. 如何加速你的PHP程序6. 資深程序員:給Python軟件開發(fā)測試的25個忠告!7. python使用tkinter實現(xiàn)透明窗體上繪制隨機出現(xiàn)的小球(實例代碼)8. python制作抽獎程序代碼詳解9. 關于PHP程序員解決問題的能力10. Python面向對象程序設計之類和對象、實例變量、類變量用法分析
排行榜
