低级错误..在线急等
import java.util.*;
class MyKey {
private String name;
private int age;
public MyKey(String name,int age)
{
this.name=name;
this.age=age;
}
public String toString()
{
return new String (name+ ", "+age);
}
public boolean equals(Object obj)
{
if(name.equals(obj.name) && age==obj.age)
return true;
else
return false;
}
public int hashCode()
{
return name.hashCode()+age;
}
}
出现错误提示
F:\javawork\HashTable> javac MyKey.java
MyKey.java:32: cannot find symbol
symbol : variable name
location: class java.lang.Object
if(name.equals(obj.name) && age==obj.age)
^
MyKey.java:32: cannot find symbol
symbol : variable age
location: class java.lang.Object
if(name.equals(obj.name) && age==obj.age)
^
2 errors
------解决方案--------------------obj.name和obj.age这个叫什么,public boolean equals(Object obj)这个方法重写的时候要把obj强制类型转化为MyKey
------解决方案--------------------if(name.equals(obj.name) && age==obj.age)之前加上MyKey mk=(MyKey)obj;之后的用mk替代