import java.util.*;
public class java12 {
private static List created=new ArrayList();
private String s;
private int id=0;
java12(String str)
{
s=str;
created.add(s);
Iterator it=created.iterator();
while(it.hasNext())
{
if(it.next().equals(s))
id++;
}
}
public String toString()
{
return "String: "+s+" id: " + id+" hashCode():"+hashCode();
}
public int hashCode()
{
int result=17;
result=37*result+s.hashCode();
result=37*result+id;
return result;
}
public boolean equals(Object o)
{
return(o instanceof java12)&&s.equals(((java12)o).s)&&id==
((java12)o).id;
}
public static void main(String[]args)
{
Map map=new HashMap();
java12[] cs=new java12[10];
for(int i=0;i<cs.length;i++)
{
cs[i]=new java12("hi");
map.put(cs[i],new Integer(i));
}
System.out.println(map);
for(int i=0;i<cs.length;i++)
{
System.out.println("Looking up"+cs[i]+" value==");
System.out.println(map.get(cs[i]));
}
}
}
请问 这个程序中的toString() ,hashCode(),eaquals()方法时如何调用的,程序中没有调用这些函数,为什么能自动调用呢?
|
|