如何在 Eclipse中更快地编写 Java …

在日常使用的 Eclipse 特性中,Source 菜单中用于代码生成的项目是用得最多的。在花了很多时间来学习如何有效使用它们后,掌握了这些特性,你就能够很快地构建 Java 类了。

例如,创建新类时,我不再花时间编写 setter 和 getter(访问器),也不用编写大部分的构造器。相反,我创建类并快速在类中输入私有变量,如下 所示。


public class test {

 private String  year;
 private String  month;
 private String  day;

 

 

然后,单击 Source > Generate Getters and Setters,选择刚才输入的、想用公共访问器公开的私有变量。要使用构造器初始化部分变量,单击 Source > Generate Constructor using Fields 以快速创建构造器。只需点击几下鼠标,一个类就差不多创建完成了,具体情况取决于要用该类实现什么功能。

如何在 <wbr>Eclipse中更快地编写 <wbr>Java <wbr>代码
public class test {
 
 
  private String  year;
  private String  month;
  private String  day;
 
  public test(String year, String month, String day) {
    super();
    this.year = year;
    this.month = month;
    this.day = day;
  }
 
  public String getYear() {
    return year;
  }
  public void setYear(String year) {
    this.year = year;
  }
  public String getMonth() {
    return month;
  }
  public void setMonth(String month) {
    this.month = month;
  }
  public String getDay() {
    return day;
  }
  public void setDay(String day) {
    this.day = day;
  }
生成 toString(),使用 Source > Generate toString() 来完成
@Override
 public String toString() {
  return "test [" + (year != null ? "year=" + year + ", " : "")
    + (month != null ? "month=" + month + ", " : "")
    + (day != null ? "day=" + day : "") + "]";
 }

 

使用 hashCode()equals()

有时,您需要创建一些规则,根据实际的字段值将您的对象变为相等的对象。这时,使用 Eclipse Galileo hashCode()equals() 生成功能真的很方便。这不同于 equals() 的默认行为,因为即使默认拥有相同值的对象也不会是相等的。

 

public static void main(String[] args) {
  test test = new test("2012", "08", "03");
  test test1 = new test("2012", "08", "03");
  System.out.println(test.toString());
  System.out.println(test1.toString());
  System.out.println("test equals test1:"+test.equals(test1));
 }

 

输出:test [year=2012, month=08, day=03]
     test [year=2012, month=08, day=03]
     test equals test1:false

可以看出所有属性值都是相同的,但是比较结果却是false。

要改变这种行为,单击 Source > Generate hashCode() and equals() 以生成 equals() 方法的一个新版本,新的方法将比较所有字段,如下所示。生成新的equals()方法:

@Override
 public boolean equals(Object obj) {
  if (this == obj)
   return true;
  if (obj == null)
   return false;
  if (getClass() != obj.getClass())
   return false;
  test other = (test) obj;
  if (day == null) {
   if (other.day != null)
    return false;
  } else if (!day.equals(other.day))
   return false;
  if (month == null) {
   if (other.month != null)
    return false;
  } else if (!month.equals(other.month))
   return false;
  if (year == null) {
   if (other.year != null)
    return false;
  } else if (!year.equals(other.year))
   return false;
  return true;
 }

再次比较结果:

test [year=2012, month=08, day=03]
test [year=2012, month=08, day=03]
test equals test1:true
通过这样,一个类便很快创建起来了,减少很多手工代码量。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值