import java.util.*;
public class HashingTest {
// instance variables
String name;
int age;
int hashCd;
String gender;
public HashingTest(String nm, int age, String gend)
{
name = nm;
this.age = age;
gender = gend;
}
public static void main(String[] args) {
HashingTest person1 = new HashingTest("Durvi",5,"Female");
HashingTest person2 = new HashingTest("Pillu",5,"Female");
HashingTest person3 = new HashingTest("Ninad",5,"Male");
HashingTest person4 = new HashingTest("Varun",5,"Male");
HashingTest person5 = new HashingTest("Sapna",5,"Female");
HashingTest person6 = new HashingTest("Priya",5,"Female");
//person2 and person1 are now referring to the same object
person2 = person1;
boolean truth = person1.equals(person2);
System.out.println(truth + " : Which means that if two object varaibles are refering the same object the equals() method returns true");
Hashtable<HashingTest, String> hs = new Hashtable<HashingTest, String>();
hs.put(person1, "Durvi");
hs.put(person2, "Pillu");
hs.put(person3, "Ninad");
hs.put(person4, "Varun");
String personVal = (String)hs.get(person1);
System.out.println(personVal);
}
}
真:這意味着,如果兩個物體varaibles是闖民宅指向同一對象equals()方法返回true Pillu
幹運行這個程序,我猜輸出是人們所期望的。你的預期產出是多少? – 2010-11-24 03:08:01