本文参考自《编写可读代码的艺术》,这本书,非常好!
简单的Java数据类,通常会有很多的成员变量,记录一种顺序,并在使用的时候坚持这种顺序,会感觉代码更有条理。
下面是一个Student类的例子
package com.jue.test; public class Student { // 一种有意义的排列顺序 public final static int INDEX_ID = 0; public final static int INDEX_NAME = 1; public final static int INDEX_PHONENUMBER = 2; public final static int INDEX_ADDRESS = 3; // 一种有意义的排列顺序 int id; String name; String phoneNumber; String address; public Student() { } public Student(int id, String name, String phoneNumber, String address) { // 一种有意义的排列顺序 this.id = id; this.name = name; this.phoneNumber = phoneNumber; this.address = address; } public Student(Student student) { // 一种有意义的排列顺序 this.id = student.id; this.name = student.name; this.phoneNumber = student.phoneNumber; this.address = student.address; } private int copyFromCursor(Cursor c) { // 一种有意义的排列顺序 this.id = c.getColumnIndexOrThrow(INDEX_ID); this.name = c.getColumnIndexOrThrow(INDEX_NAME); this.phoneNumber = c.getColumnIndexOrThrow(INDEX_PHONENUMBER); this.address = c.getColumnIndexOrThrow(INDEX_ADDRESS); } }