要求:Below is a class named Demo ,please add equals(),hashCode() and toString() methods to it.
class Demo{
private int a;
private String b; Demo()
{
super();
a=100;
b="hello world!";
}
}
这是昨天面试爱立信的一道java题,因为本人对Java涉及较少,希望大家帮忙!
先谢谢了!
class Demo{
private int a;
private String b; Demo()
{
super();
a=100;
b="hello world!";
}
}
这是昨天面试爱立信的一道java题,因为本人对Java涉及较少,希望大家帮忙!
先谢谢了!
//要求:Below is a class named Demo ,please add equals(),hashCode() and toString() methods to it.
public class Demo { private int a;
private String b; Demo()
{
super();
a=100;
b="hello world!";
} @Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
return super.equals(obj);
} @Override
public int hashCode() {
// TODO Auto-generated method stub
return super.hashCode();
} @Override
public String toString() {
// TODO Auto-generated method stub
return super.toString();
}
}
public class Demo {
private int a;
private String b; Demo() {
super();
a=100;
b="hello world!";
}
/**
* 重写toString方法
*/
public String toString() {
return "a="+this.getA()+";b="+this.getB();
}
/**
* 重写equals方法
*/
public boolean equals(Object obj) {
Demo demo = (Demo)obj;
if(demo.getA() == this.getA() && demo.getB() == this.getB()) {
return true;
}
return false;
}
/**
* 重写hashCode方法
*/
public int hashCode() {
int result = 17;
result = 37 * result + this.getA();
result = 37 * result + this.getB().hashCode();
return result;
}
public static void main(String[] args) {
Demo d1 = new Demo();
Demo d2 = new Demo();
System.err.println(d1.hashCode());
System.err.println(d2.hashCode());
System.err.println(d1.equals(d2) );
} public int getA() {
return a;
} public void setA(int a) {
this.a = a;
} public String getB() {
return b;
} public void setB(String b) {
this.b = b;
}
}
重写对象的equals方法必须要重写hashCode方法。为的是保证“相等的对象必须具有相等的散列码(哈希吗)”
/**
* 重写equals方法
*/
public boolean equals(Object obj) {
Demo demo = (Demo)obj;
if(demo.getA() == this.getA() && demo.getB() == this.getB()) {
return true;
}
return false;
}
应该改为 /**
* 重写equals方法
*/
public boolean equals(Object obj) {
Demo demo = (Demo)obj;
if(demo.getA() == this.getA() && demo.getB().equals(this.getB())) {
return true;
}
return false;
}
{
if (obj == null) return false;
if (!(obj instanceof FieldPosition))
return false;
FieldPosition other = (FieldPosition) obj;
if (attribute == null) {
if (other.attribute != null) {
return false;
}
}
else if (!attribute.equals(other.attribute)) {
return false;
}
return (beginIndex == other.beginIndex
&& endIndex == other.endIndex
&& field == other.field);
}这个题目的答案应该是这样的吧
{
private int a;
private String b;
Demo()
{
super();
a=100;
b="hello world!";
}
public String getString()
{
return b;
}
public int getInt()
{
return a;
}
public boolean equals(Object other)
{
if(this==other)
return true;
if(other==null)
return false;
if(getClass()==other.getClass())
return true;
Demo demo=(Demo)other;
return a==demo.a&&b.equals(demo.b);
}
public String toString()
{
return super.toString()+"a="+a+"b="+b;
}
public int hashCode()
{
return 5*new Integer(a).hashCode()+6*b.hashCode();
}
} 我是这么想的,仅供参考。如有不对之处多加指点。