class A
{
int count;
public A(int count)
{
this.count = count;
}
public boolean equals(Object obj)
{
if (obj == this)       //这一句有何作用     这里的this代表什么
{
return true;
}
if (obj != null && 
obj.getClass() == A.class) //这行有错吗?      A.class为什么不是A.getClass
{
A a = (A)obj;
if (this.count == a.count)//结合这三句ht.put(new A(60000) , "Struts2权南");
{                         //ht.put(new A(87563) , "轻量级J2EE企业应用实战");
return true;      //ht.put(new A(1232) , new B());
}                         //麻烦帮我分析一下this.count == a.count的作用
}
return false;
}
public int hashCode()
{
return this.count;
}
}
class B
{
public boolean equals(Object obj)
{
return true;
}
}
public class TestHashtable
{
public static void main(String[] args) 
{
Hashtable ht = new Hashtable();
ht.put(new A(60000) , "Struts2权威指南");
ht.put(new A(87563) , "轻量级J2EE企业应用实战");
ht.put(new A(1232) , new B());
System.out.println(ht);
class A
{
int count;
public A(int count)
{
this.count = count;
}
public boolean equals(Object obj)
{
if (obj == this)
{
return true;
}
if (obj != null && 
obj.getClass() == A.class)
{
A a = (A)obj;
if (this.count == a.count)
{
return true;
}
}
return false;
}
public int hashCode()
{
return this.count;
}
}
class B
{
public boolean equals(Object obj)
{
return true;
}
}
public class TestHashtable
{
public static void main(String[] args) 
{
HashMap ht = new HashMap();
ht.put(new A(60000) , "Struts2权威指南");
ht.put(new A(87563) , "轻量级J2EE企业应用实战");
ht.put(new A(1232) , new B());
System.out.println(ht);

System.out.println(ht.containsValue("测试字符串"));


System.out.println(ht.containsKey(new A(87563)));

ht.remove(new A(1232));
for (Object key : ht.keySet())
{
System.out.print(key + "---->");
System.out.print(ht.get(key) + "\n");
}
}
}

System.out.println(ht.containsValue("测试字符串"));

System.out.println(ht.containsKey(new A(87563)));

ht.remove(new A(1232));
for (Object key : ht.keySet())
{
System.out.print(key + "---->");
System.out.print(ht.get(key) + "\n");
}
}
}
《疯狂JAVA讲义上的》      A类里面重写的equal()方法什么意思啊  看不懂
仅仅就是equal()那里不懂   HashMap那里相关的知识都是知道的    
   求救!!!!!求救!!!!!

解决方案 »

  1.   

    if (obj == this) //这一句有何作用 这里的this代表什么
    this代表当前对象,这句是判断需要比较的obj和当前对象是不是同一对象obj.getClass() == A.class) //这行有错吗? A.class为什么不是A.getClass
    obj和A一个是对象,一个是类,用法不一样,都是取运行时类//麻烦帮我分析一下this.count == a.count的作用
    这句话是看需要比较的对象和当前对象的count值是否相同,如果相同,认为两个对象是相等的。
      

  2.   

    LS都已经说了。
    obj.getClass() == A.class 这里感觉改为 obj instanceof A 要好一些
    ht.put(new A(60000) , "Struts2权威指南"); //这里生成一个A实例,它的count是60000,这个A实例作为HashMap集合的一个元素的key被保存,其他同理
    所以
    ht.remove(new A(1232));会删除HashMap集合的key为count=1232的A实例的元素
      

  3.   

    A类重写的equals是基于以下原则来判断:1)若两个对象是同一对象,返回true
    2) 若两个对象都是A类的实例,且它们的count相等,返回true
    否则返回false这种判断原则是你可以自己定义的,取决于实际情况的需要。在本例情况下,事实上也可以省略掉以上的步骤1,效果是一样的
      

  4.   

    1楼说的很明确了。还有就是你的Hashtable,重写equals为了ht.remove(new A(1232));会删除HashMap集合的key为count=1232的A实例的元素
     也就是if (obj == this) //这一句有何作用 这里的this代表什么
    this代表当前对象,这句是判断需要比较的obj和当前对象是不是同一对象obj.getClass() == A.class) //这行有错吗? A.class为什么不是A.getClass
    obj和A一个是对象,一个是类,用法不一样,都是取运行时类//麻烦帮我分析一下this.count == a.count的作用