但是却没有办法让一个类型的对象调用其父类型的函数,也就是关闭动态绑定,我看thinking in java说把函数声明为final可以关闭动态绑定,但是只是告诉compiler:动态绑定不需要,允许compiler根据final method call产生效率较佳的的程序代码,但是并不能保证compiler不使用动态绑定
你知道String的hashCode()方法是怎么算出来的吗?
Returns a hash code for this string. The hash code for a String object is computed as
s[0]*31^(n-1) + s[1]*31^(n-2) + ... + s[n-1
我说的是使用java.lang.object.hashCode();
我刚学java,记得书中介绍说java对java.lang.object.hashCode();是通过返回对象在内存中的地址来实现的。
public class StrAddr {
public static void main(String[] args) {
String a = "123";
String b = "456";
System.out.println("String Value Address: " + Integer.toHexString(a.hashCode()));
System.out.println("String Value Address: " + Integer.toHexString(b.hashCode()));
}
}
public class InfiniteRecursion {
public String toString() {
return "address: " + Integer.toHexString(hashCode()) + "\n";
}
public static void main(String[] args) {
List v = new ArrayList();
for (int i = 0; i < 10; i++)
v.add(new InfiniteRecursion());
System.out.println(v);
}
}