文章详情页
Java程序的编码规范(3)
浏览:154日期:2024-07-03 09:26:15
内容: Java程序的编码规范(3)作者:李小敏 本文选自:IBM DW中国 2002年08月21日 ● 构造函数 接下来是构造函数,它应该用递增的方式写(比如:参数多的写在后面)。 访问类型 ('public', 'private' 等.) 和 任何 'static', 'final' 或 'synchronized' 应该在一行中,并且方法和参数另写一行,这样可以使方法和参数更易读。 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. Java程序员可能犯的3个常见SQL错误3. python解析PDF程序代码4. Java面向对象程序设计:继承,多态用法实例分析5. python GUI编程(Tkinter) 创建子窗口及在窗口上用图片绘图实例6. Python聊天室带界面实现的示例代码(tkinter,Mysql,Treading,socket)7. java中long(Long)与int(Integer)之间的转换方式8. 详解python tkinter包获取本地绝对路径(以获取图片并展示)9. python基于tkinter制作下班倒计时工具10. Python测试开源工具splinter安装与使用教程
排行榜