0Pricing
Java Academy · 课时

this 与 toString/equals/hashCode

使用 this 引用当前对象,编写实用的 toString,并正确实现 equals 和 hashCode。

this 与 toString/equals/hashCode 是 CoddyKit 上的免费 Java Academy 课时。 这是第 1 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Java Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Java Academy 课程共包含 3 节课。

this 关键字

this 指向当前对象。您可以使用它访问字段或调用另一个构造方法。当参数名称与字段名称相同时,使用它可以明确表达您的意图。

可读的 toString

toString() 返回对象的易读摘要。请重写它,以显示标识符和名称等关键字段。这样可以让日志和调试输出更容易理解。

equals 与 ==

equals 检查逻辑相等性,而 == 检查两个引用是否指向同一个对象。大多数领域类都应该重写 equals,按内容而不是内存地址进行比较。

hashCode 与 equals 保持一致

hashCode 必须与 equals 保持一致:如果 a.equals(b) 为 true,那么 a.hashCode() 必须等于 b.hashCode()。HashSet 和 HashMap 正确工作时必须遵守这条规则。

提示与注意事项

请选择稳定的字段进行相等性判断,例如标识符或邮箱,并让 toString 保持简短。请避免在 equals 和 hashCode 中使用可变集合。如果不确定,可以使用集成开发环境生成方法,然后进行检查。

代码:this、equals、hashCode

该演示创建了两个内容相同的 Person 对象。equals 返回 true,哈希码相同,并且 HashSet 可以找到第二个对象。

public class Main {
  // Simple Person class to demo this, toString, equals, hashCode
  static class Person {
    private int id;
    private String name;

    // Constructor uses this to set fields
    Person(int id, String name) {
      this.id = id;       // this.id refers to the field
      this.name = name;   // name comes from the parameter
    }

    // Human friendly string
    @Override
    public String toString() {
      return "Person{id=" + id + ", name=" + name + "}";
    }

    // Logical equality: same id and same name
    @Override
    public boolean equals(Object o) {
      if (this == o) return true;                 // same reference
      if (o == null || getClass() != o.getClass()) return false;
      Person other = (Person) o;
      if (this.id != other.id) return false;
      // Compare names safely (null aware)
      if (this.name == null) return other.name == null;
      return this.name.equals(other.name);
    }

    // Hash code consistent with equals
    @Override
    public int hashCode() {
      int result = Integer.hashCode(id);
      result = 31 * result + (name == null ? 0 : name.hashCode());
      return result;
    }
  }

  public static void main(String[] args) {
    // Create two persons with the same content
    Person a = new Person(1, "Ada");
    Person b = new Person(1, "Ada");

    // toString shows readable info
    System.out.println(a);

    // equals compares content, not memory address
    System.out.println("a == b ? " + (a == b));           // false (different objects)
    System.out.println("a.equals(b) ? " + a.equals(b));   // true (same content)

    // hashCode must match for equal objects (useful in hash collections)
    System.out.println("hash(a) = " + a.hashCode());
    System.out.println("hash(b) = " + b.hashCode());

    // Example with HashSet: contains works because equals and hashCode agree
    java.util.Set<Person> set = new java.util.HashSet<>();
    set.add(a);
    System.out.println("set contains b ? " + set.contains(b)); // true
  }
}

契约检查

快速检查:如果重写了 equals,还应该做什么?为什么?

回顾

回顾:this 可以明确字段访问,toString 有助于调试,equals 比较内容,而 hashCode 确保集合保持一致。

常见问题解答

「this 与 toString/equals/hashCode」课时是免费的吗?

是的 — 「this 与 toString/equals/hashCode」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Java Academy 课程的其余内容,请升级到 CoddyKit PRO。 Java Academy 课程共包含 3 节课。

「this 与 toString/equals/hashCode」这节课中我会学到什么?

使用 this 引用当前对象,编写实用的 toString,并正确实现 equals 和 hashCode。 你通过在浏览器中直接运行的动手代码来练习 Java Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Java Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Java Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 3 节。

「this 与 toString/equals/hashCode」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Java Academy 课中编写并运行代码吗?

能。每节 Java Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. this 与 toString/equals/hashCode
  2. 对象组合
  3. 领域建模(UML→代码)
← 返回 Java Academy