本文参考自《编写可读代码的艺术》,这本书写的非常好!
有时候,我们可以通过“列对齐"的方式,让代码更易读。实例代码如下:
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); } public static void main(String args[]){ Student xiaoming = new Student(0, "张小明", "15010112299", "新疆维吾尔自治区"); Student wanggang = new Student(1, "王刚", "13900000099", "北京市"); Student liyue = new Student(2, "李越", "18510112299", "西藏藏族自治区"); Student wangli = new Student(3, "王丽", "13588112299", "云南省"); } }