实例化内部类 [英] Instantiating inner class
问题描述
我正在处理一个覆盖 hashCode 和 equals 方法的示例问题,但收到错误:没有可访问类型 CustomHashCodeExample 的封闭实例.必须使用 CustomHashCodeExample 类型的封闭实例限定分配(egxnew A() 其中 x 是 CustomHashCodeExample 的实例."我写了一个内部类 HashPerson,当我尝试在另一个名为 testHashCodeOverride() 的方法中实例化这个内部类时出现这个错误.
I working on a sample problem of over-ridding hashCode and equals method but getting an error: "No enclosing instance of type CustomHashCodeExample is accessible. Must qualify the allocation with an enclosing instance of type CustomHashCodeExample (e.g. x.new A() where x is an instance of CustomHashCodeExample)." I wrote an inner class HashPerson and I am getting this error when I am trying to instantiate this inner class in another method called testHashCodeOverride().
public static void testHashCodeOverride(){
System.out.println("
Test HashCode Override Method");
System.out.println("==================================
");
HashPerson william = new HashPerson("willy");
HashPerson bill = new HashPerson("willy");
}
这段代码工作正常,即使我没有看到静态内部类或外部类的实例化,也很困惑:(
This code works fine, even though I dont see static inner class or instantiation of outer class, confused :(
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 + " not found");
}
if (table.containsKey(bill)) {
System.out.println(table.get(bill));
} else {
System.out.println("Key " + bill + " not found");
}
}
class HashPerson {
private static final int HASH_PRIME = 1000003;
public HashPerson(String name) {
this.name = name;
}
public String toString() {
return name;
}
public boolean equals(Object rhs) {
if (this == rhs)
return true;
// make sure they are the same class
if (rhs == null || rhs.getClass() != getClass())
return false;
// ok, they are the same class. Cast rhs to HashPerson
HashPerson other = (HashPerson) rhs;
// our test for equality simply checks the name field
if (!name.equals(other.name)) {
return false;
}
// if we get this far, they are equal
return true;
}
public int hashCode() {
int result = 0;
result = HASH_PRIME * result + name.hashCode();
return result;
}
private String name;
}
}
推荐答案
我认为您想将 HashPerson
类声明为 static
.否则它只能在包含类的上下文中实例化,或者在包含类的方法中或者使用这样的代码:
I think you want to declare the HashPerson
class as static
. Otherwise it can only be instantiated in the context of the containing class, either in a method of the containing class or using code like this:
ContainingClass container = new ContainingClass();
HashPerson william = container.new HashPerson("willy");
实际上,我的经验法则是将任何嵌套类设为静态,除非我有特殊原因不这样做.这也更有效,因为非静态嵌套类(称为内部类)总是包含对包含对象的隐式引用.
Actually, my rule-of-thumb is to make any nested class static, unless I have a special reason not to. This is also more efficient, because non-static nested classes (called inner classes) always contain an implicit reference to the containing object.
这篇关于实例化内部类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!