import java.util.*;public class BasicContainer {
public static void main (String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
c.remove (new Name("f1","l1"));
System.out.println(c);
}
}class Name{
private String firstName,lastName;
public Name(String firstName,String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getfirstName() {return firstName;}
public String getlastName() {return lastName;}
public String toString () {return firstName + " "+lastName;}
} public boolean equals(Object obj) {
if (obj instanceof Name) {
Name name = (Name) obj;
return (firstName.equals(name.firstName))
&& (lastName.equals(name.lastName));
}
return super.equals(obj);
}
public int hashCode() {
return firstName.hashCode();
}
public static void main (String[] args) {
Collection c = new ArrayList();
c.add("hello");
c.add(new Name("f1","l1"));
c.add(new Integer(100));
c.remove("hello");
c.remove(new Integer(100));
c.remove (new Name("f1","l1"));
System.out.println(c);
}
}class Name{
private String firstName,lastName;
public Name(String firstName,String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getfirstName() {return firstName;}
public String getlastName() {return lastName;}
public String toString () {return firstName + " "+lastName;}
} public boolean equals(Object obj) {
if (obj instanceof Name) {
Name name = (Name) obj;
return (firstName.equals(name.firstName))
&& (lastName.equals(name.lastName));
}
return super.equals(obj);
}
public int hashCode() {
return firstName.hashCode();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货