public class Number1 {
public static void main(String[] args) {
Collection<String> con=new HashSet<String>();
con.add("hello");
con.add(new Name("hi","ji"));//add显示错误
con.add(new Name("fi","ki"));//add显示错误
System.out.print(con);
}
}
class Name implements Comparable{
String firshname,lastname;
Name(String firshname,String lastname){
this.firshname=firshname;
this.lastname=lastname;
}
public String getfirshname(){
return firshname;
}
public String getlastname(){
return lastname;
}
public String toString(){
return firshname+" "+lastname;
}
@Override
public int compareTo(Object o) {
Name n=(Name)o;
int lastcom=lastname.compareTo(n.lastname);
return (lastcom !=0 ?lastcom : firshname.compareTo(n.firshname));
}
}
当Collection<String> con=new HashSet<String>();这句中去掉<String>,换成
Collection con=new HashSet();就可以运行啦!请问为什么?
|
|