...
Code Block | ||
---|---|---|
| ||
import java.util.Map;
import java.util.HashMap;
public final class CreditCard {
private final int number;
public CreditCard(int number) {
this.number = (short) number;
}
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (!(o instanceof CreditCard)) {
return false;
}
CreditCard cc = (CreditCard)o;
return cc.number == number;
}
public int hashCode() {
int result = 7;
result = 37 * result + number;
return result;
}
public static void main(String[] args) {
Map<CreditCard, String> m = new HashMap<CreditCard, String>();
m.put(new CreditCard(100), "Java");
System.out.println(m.get(new CreditCard(100)));
}
}
|
...