文章詳情頁
Java程序的編碼規(guī)范(3)
瀏覽:112日期:2024-07-03 09:26:15
內(nèi)容: Java程序的編碼規(guī)范(3)作者:李小敏 本文選自:IBM DW中國 2002年08月21日 ● 構(gòu)造函數(shù) 接下來是構(gòu)造函數(shù),它應(yīng)該用遞增的方式寫(比如:參數(shù)多的寫在后面)。 訪問類型 ('public', 'private' 等.) 和 任何 'static', 'final' 或 'synchronized' 應(yīng)該在一行中,并且方法和參數(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 方法 無論如何,每一個類都應(yīng)該定義 toString 方法: publicString toString() { String retval = 'CounterSet: '; for (int i = 0; i
標(biāo)簽:
Java
相關(guān)文章:
1. IntelliJ IDEA2021.2.3破解,IDEA 2021.2.x激活破解,2022激活持續(xù)更新2. IntelliJ IDEA安裝插件的方法步驟3. IntelliJ IDEA刪除類的方法步驟4. IntelliJ IDEA 2020常用配置設(shè)置大全(方便干活)5. IntelliJ IDEA恢復(fù)刪除文件的方法6. IDEA打包應(yīng)用程序的教程圖解7. IntelliJ IDEA創(chuàng)建web項(xiàng)目的方法8. 教你在 IntelliJ IDEA 中使用 VIM插件的詳細(xì)教程9. IntelliJ IDEA導(dǎo)入項(xiàng)目的方法10. IntelliJ IDEA設(shè)置背景圖片的方法步驟
排行榜
