看板 java 關於我們 聯絡資訊
Earvin 提出了疑問, 我自己也試了 equals/hashCode 的實作有問題 所以我改了一下 import java.util.HashSet; public class Pair<M, N> extends HashSet{ public Pair(M m, N n) { this.add(m); this.add(n); } @Override public boolean equals(final Object other) { if (!(other instanceof Pair)) return false; return super.equals(other); } @Override public int hashCode() { return super.hashCode(); } public static void main(String[] args) { boolean b = new Pair<Integer, Integer>(1234567, 7654321) .equals(new Pair<Integer, Integer>(7654321, 1234567)); System.out.println(b); // true // true System.out.println( new Pair("a", "b").equals(new Pair("b", "a"))); } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 220.133.80.216
Nt1:謝謝,我會試試看的! 05/16 01:43