在 Eclipse Galileo 中更快地编写 Java 代码
2009-09-22 18:03
232 查看
学习如何在 Eclipse Galileo 中使用新的 toString() 代码生成功能以及 hashCode()、equals() 和 setter/getter 生成功能来减少为 Java™ 类构建基础所需的工作量。
这个代码生成技巧使用 Eclipse Galileo 中的新特性。但是,您也可以使用在这里介绍的、旧版本 Eclipse(如 Ganymede)中的某些技巧(如生成 getters 和 setters)。
代码生成概述
在日常使用的 Eclipse 特性中,Source 菜单中用于代码生成的项目是用得最多的。我花了很多时间来学习如何有效使用它们,但是掌握了这些特性后,我就能够很快地构建 Java 类了。
例如,创建新类时,我不再花时间编写 setter 和 getter(访问器),也不用编写大部分的构造器。相反,我创建类并快速在类中输入私有变量,如清单 1 所示。
清单 1. 私有变量
然后,单击 Source > Generate Getters and Setters
,选择刚才输入的、想用公共访问器公开的私有变量。要使用构造器初始化部分变量,单击 Source > Generate Constructor using Fields
以快速创建构造器。只需点击几下鼠标,一个类就差不多创建完成了,具体情况取决于要用该类实现什么功能(见清单 2)。理想的情况是,新创建的代码应该遵守此前在 Eclipse 首选项中设置的代码格式规则。
清单 2. 自动创建构造器和访问器
除了可以用 Source 菜单项生成代码外,还可以通过使用 Ctrl+Space
快捷键编写许多公共代码块来生成大量代码。要弄清生成特定的代码块需要使用的名称,查看 Preferences
窗口。例如,输入 lazy,然后按下 Ctrl+Space
,这将生成用于延迟加载的 Java 代码。
代码生成设置
新方法的代码生成设置在 Java > Code Style > Code Templates
下面的 Preferences
窗口中修改。
图 1. Java > Code Style > Code Templates 首选项
[Pic]
Preferences
窗口中的 Java > Editor > Code Templates
设置根据名称列示模板(见图 2)。仔细检查 Eclipse 带有的模板。您可以添加自己的模板,也可以导入模板。
图 2. Java > Editor > Code Templates 首选项
[Pic]
使用
toString() 生成功能
Eclipse Galileo 中的一个新特性是能够生成 toString() 方法。默认情况下,toString() 方法输出一个类的表示,这也许不能真正显示您想要查看的属性。检查清单 3 中的 main 方法。
清单 3. 使用
toString()
输出 Automobile
这个应用程序的输出如清单 4 所示。
清单 4.
Main
方法的输出
在 Galileo 之前,我必须手工编写 toString() 方法。尽管编写这个小类并不是太费力,但如果一个类拥有许多字段,编写工作就会花费一些时间。我可能想做很多事情(比如检查一些空值),而不仅仅是串联一些值。也许我想使用一个 StringBuilder 来获得更好的性能。但是对于 Galileo,我可以使用 Source > Generate toString()
来完成上述所有任务,如图 3 所示。
图 3. 生成
toString()
[Pic]
单击 Finish
之后,新的 toString() 方法如清单 5 所示。
清单 5. 自动生成的
toString()
方法
现在,main 方法运行时,输出如清单 6 所示。
清单 6. 自动生成的
toString()
方法的输出
格式化字符串
即使清单 6 中显示的输出比清单 4 中的原始输出更具描述性,但调整一下输出格式使其更具可读性可能会更好,比如 “1993 Toyota Corolla (Automobile)”。自定义模板允许您调整 toString() 方法生成的输出。
删除 toString() 方法并再次单击 Source > Generate toString()
。这次:
单击 String format
下拉列表旁边的 Edit
。
单击 New
。
图 4. 添加一个新格式
[Pic]
在 Pattern
中输入类似 ${member.value} ${otherMembers} (${object.className}) 的内容,给它起个名称,然后单击 OK
。
选中新模式,在 Generate toString()
上单击 OK
。新代码如清单 7 所示。
清单 7. 更新后的
toString()
方法
现在,运行 main 方法,Automobile 对象的 toString() 输出如清单 8 所示。
清单 8. Automobile 对象的
toString()
方法的输出
处理数组
您还能使用 toString() 生成器处理数组。清单 9 展示了一个称为 options
的新的字符串数组。
清单 9. options 字符串数组
通常,原生 toString() 方法输出的数组表示就像对象的原始表示一样,并不真正显示内容。但是,选项 List contents of arrays instead of using native toString
改变了这种情况。选中该选项,重新生成 toString() 方法,新的输出如清单 10 所示。
清单 10. 重新生成的
toString()
方法的输出
限制输出
如果某些数组非常大,可以通过选中 Limit number of items in arrays/collections/maps
并设置限制来约束输出的内容(见图 5)。这样做可以阻止 toString() 方法输出过多内容。
图 5. 输出数组内容并施加限制
http://cid-8b496f13e8d06fdc.skydrive.live.com/self.aspx/.Public/zhouyi.jpg
清单 11 显示了将限制设置为 2 的情况。
清单 11. 将数组内容限制为 2
使用
hashCode() 和 equals()
有时,您需要创建一些规则,根据实际的字段值将您的对象变为相等的对象。这时,使用 Eclipse Galileo hashCode() 和 equals() 生成功能真的很方便。这不同于 equals() 的默认行为,因为即使默认拥有相同值的对象也不会是相等的。看看清单 12 中的代码。
清单 12.
equals()
方法的默认行为
即使所有属性值设置为相同的值,代码执行时也会输出 Auto 1 and 2 are equal(): false。
要改变这种行为,单击 Source > Generate hashCode() and equals()
以生成 equals() 方法的一个新版本,新的方法将比较所有字段,如下所示。
清单 13. 用新的
equals()
方法比较所有字段
现在,执行这段代码时,这两个对象使用覆盖后的 equals() 方法进行比较,所以二者是相同的。
Eclipse Galileo 中的另一个新特性是为 if 语句生成代码块的能力。如果您还没有将 Source > Clean Up
配置为将单行 if 语句转换为代码块,这个新特性很有用。避免使用单行 if 语句通常被认为是最佳实践,因此许多人愿意确保他们的代码遵守这个最佳实践。
这个代码生成技巧使用 Eclipse Galileo 中的新特性。但是,您也可以使用在这里介绍的、旧版本 Eclipse(如 Ganymede)中的某些技巧(如生成 getters 和 setters)。
代码生成概述
在日常使用的 Eclipse 特性中,Source 菜单中用于代码生成的项目是用得最多的。我花了很多时间来学习如何有效使用它们,但是掌握了这些特性后,我就能够很快地构建 Java 类了。
例如,创建新类时,我不再花时间编写 setter 和 getter(访问器),也不用编写大部分的构造器。相反,我创建类并快速在类中输入私有变量,如清单 1 所示。
清单 1. 私有变量
public class Automobile { private String make; private String model; private String year; } |
,选择刚才输入的、想用公共访问器公开的私有变量。要使用构造器初始化部分变量,单击 Source > Generate Constructor using Fields
以快速创建构造器。只需点击几下鼠标,一个类就差不多创建完成了,具体情况取决于要用该类实现什么功能(见清单 2)。理想的情况是,新创建的代码应该遵守此前在 Eclipse 首选项中设置的代码格式规则。
清单 2. 自动创建构造器和访问器
public class Automobile { private String make; private String model; private String year; public String getMake() { return make; } public Automobile(String make, String model, String year) { super(); this.make = make; this.model = model; this.year = year; } public void setMake(String make) { this.make = make; } public String getModel() { return model; } public void setModel(String model) { this.model = model; } public String getYear() { return year; } public void setYear(String year) { this.year = year; } } |
快捷键编写许多公共代码块来生成大量代码。要弄清生成特定的代码块需要使用的名称,查看 Preferences
窗口。例如,输入 lazy,然后按下 Ctrl+Space
,这将生成用于延迟加载的 Java 代码。
代码生成设置
新方法的代码生成设置在 Java > Code Style > Code Templates
下面的 Preferences
窗口中修改。
图 1. Java > Code Style > Code Templates 首选项
[Pic]
|
窗口中的 Java > Editor > Code Templates
设置根据名称列示模板(见图 2)。仔细检查 Eclipse 带有的模板。您可以添加自己的模板,也可以导入模板。
图 2. Java > Editor > Code Templates 首选项
[Pic]
使用
toString() 生成功能
Eclipse Galileo 中的一个新特性是能够生成 toString() 方法。默认情况下,toString() 方法输出一个类的表示,这也许不能真正显示您想要查看的属性。检查清单 3 中的 main 方法。
清单 3. 使用
toString()
输出 Automobile
public class Main { public static void main(String[] args) { Automobile auto = new Automobile("Toyota", "Corolla", "1993"); System.out.println(auto.toString()); } } |
清单 4.
Main
方法的输出
Automobile@77df38fd |
来完成上述所有任务,如图 3 所示。
图 3. 生成
toString()
[Pic]
单击 Finish
之后,新的 toString() 方法如清单 5 所示。
清单 5. 自动生成的
toString()
方法
... @Override public String toString() { return "Automobile [" + (make != null ? "make=" + make + ", " : "") + (model != null ? "model=" + model + ", " : "") + (year != null ? "year=" + year : "") + "]"; } ... |
清单 6. 自动生成的
toString()
方法的输出
Automobile [make=Toyota, model=Corolla, year=1993] |
即使清单 6 中显示的输出比清单 4 中的原始输出更具描述性,但调整一下输出格式使其更具可读性可能会更好,比如 “1993 Toyota Corolla (Automobile)”。自定义模板允许您调整 toString() 方法生成的输出。
删除 toString() 方法并再次单击 Source > Generate toString()
。这次:
单击 String format
下拉列表旁边的 Edit
。
单击 New
。
图 4. 添加一个新格式
[Pic]
在 Pattern
中输入类似 ${member.value} ${otherMembers} (${object.className}) 的内容,给它起个名称,然后单击 OK
。
选中新模式,在 Generate toString()
上单击 OK
。新代码如清单 7 所示。
清单 7. 更新后的
toString()
方法
... @Override public String toString() { return (make != null ? make + " " : "") + (model != null ? model + " " : "") + (year != null ? year : "") + " (Automobile)"; } ... |
清单 8. Automobile 对象的
toString()
方法的输出
Toyota Corolla 1993 (Automobile) |
您还能使用 toString() 生成器处理数组。清单 9 展示了一个称为 options
的新的字符串数组。
清单 9. options 字符串数组
Automobile auto = new Automobile("Toyota", "Corolla", "1993"); String[] options = new String[] { "Automatic Transmission", "Power Brakes", "Power Windows" }; // new generated method after adding private String[] options; auto.setOptions(options); System.out.println(auto.toString()); // prints this: // Toyota Corolla [Ljava.lang.String;@defb836 1993 (Automobile) |
改变了这种情况。选中该选项,重新生成 toString() 方法,新的输出如清单 10 所示。
清单 10. 重新生成的
toString()
方法的输出
Toyota Corolla [Automatic Transmission, Power Brakes, Power Windows] 1993 (Automobile) |
如果某些数组非常大,可以通过选中 Limit number of items in arrays/collections/maps
并设置限制来约束输出的内容(见图 5)。这样做可以阻止 toString() 方法输出过多内容。
图 5. 输出数组内容并施加限制
http://cid-8b496f13e8d06fdc.skydrive.live.com/self.aspx/.Public/zhouyi.jpg
清单 11 显示了将限制设置为 2 的情况。
清单 11. 将数组内容限制为 2
Toyota Corolla [Automatic Transmission, Power Brakes] 1993 (Automobile) |
hashCode() 和 equals()
有时,您需要创建一些规则,根据实际的字段值将您的对象变为相等的对象。这时,使用 Eclipse Galileo hashCode() 和 equals() 生成功能真的很方便。这不同于 equals() 的默认行为,因为即使默认拥有相同值的对象也不会是相等的。看看清单 12 中的代码。
清单 12.
equals()
方法的默认行为
public class Main { public static void main(String[] args) { Automobile auto = new Automobile("Toyota", "Corolla", "1993"); String[] options = new String[] { "Automatic Transmission", "Power Brakes", "Power Windows" }; auto.setOptions(options); Automobile auto2 = new Automobile("Toyota", "Corolla", "1993"); String[] options2 = new String[] { "Automatic Transmission", "Power Brakes", "Power Windows" }; auto2.setOptions(options2); System.out.println("Autos 1 and 2 are equal(): " + auto.equals(auto2)); } } |
要改变这种行为,单击 Source > Generate hashCode() and equals()
以生成 equals() 方法的一个新版本,新的方法将比较所有字段,如下所示。
清单 13. 用新的
equals()
方法比较所有字段
@Override public int hashCode() { // snipped... } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Automobile other = (Automobile) obj; if (make == null) { if (other.make != null) return false; } else if (!make.equals(other.make)) return false; if (model == null) { if (other.model != null) return false; } else if (!model.equals(other.model)) return false; if (!Arrays.equals(options, other.options)) return false; if (year == null) { if (other.year != null) return false; } else if (!year.equals(other.year)) return false; return true; } |
Eclipse Galileo 中的另一个新特性是为 if 语句生成代码块的能力。如果您还没有将 Source > Clean Up
配置为将单行 if 语句转换为代码块,这个新特性很有用。避免使用单行 if 语句通常被认为是最佳实践,因此许多人愿意确保他们的代码遵守这个最佳实践。
相关文章推荐
- 在 Eclipse Galileo 中更快地编写 Java 代码使用新的 toString() 生成器
- 在 Eclipse Galileo 中更快地编写 Java 代码
- 在 Eclipse Galileo 中更快地编写 Java 代码
- Eclipse下编写HTML/CSS/JS/JSP/Java代码时,自动提示代码
- Eclipse 在编写java代码时打印每个字母都会提示的设置方法
- 解决Ubuntu下的Eclipse打开Windows编写的java代码的中文乱码
- 在Eclipse下编写HTML/CSS/JS/JSP/Java代码时,代码自动提示的相关设置
- 利用eclipse编写高质量的java代码
- 在Eclipse下编写HTML/CSS/JS/JSP/Java代码时,代码自动提示的相关设置
- 关于eclipse编写java代码时不能运行for-each语句的问题
- Java代码编写的30条建议
- ZT:Java代码编写的30条建议
- java eclipse 注释代码快捷键 取消代码注释快捷键
- Eclipse编写XML代码的时候没有代码提示
- eclipse代码编写过程自动提示
- eclipse中写java,html,css,javascript代码提示
- 编写高质量代码:改善Java程序的151个建议(第7章:泛型和反射___建议98~101)
- java代码编写出现的陷阱-2:阴沟里翻船
- 在Eclipse中运行JAVA代码远程操作HBase的示例