我正在研究过度散列hashCode和equals方法的示例问题,但是得到一个错误:" 没有可以访问CustomHashCodeExample类型的封闭实例.必须使用CustomHashCodeExample类型的封闭实例来限定分配(例如,xnew A(),其中x是一个实例of CustomHashCodeExample). "我写了一个内部类HashPerson,当我试图在另一个名为testHashCodeOverride()的方法中实例化这个内部类时,我收到了这个错误.
public static void testHashCodeOverride(){
System.out.println("\nTest HashCode Override Method");
System.out.println("==================================\n");
HashPerson william = new HashPerson("willy");
HashPerson bill = new HashPerson("willy");
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常,即使我没有看到静态内部类或外部类的实例化,困惑:(
public class HashCodeExample {
public static void testHashCodeOverride() {
HashPerson william = new HashPerson("Willy");
HashPerson bill = new HashPerson("Willy");
System.out.println("Hash code for william = " + william.hashCode());
System.out.println("Hash code for bill = " + bill.hashCode());
HashMap table = new HashMap();
table.put(william, "Silly");
if (table.containsKey(william)) {
System.out.println(table.get(william));
} else {
System.out.println("Key " + william + …Run Code Online (Sandbox Code Playgroud)