本帖最后由 rolan 于 2015-4-29 22:52 编辑
如题,我在编码的时候出现了一个我查不出来的错误,请大家帮我看看,代码如下
- import java.util.HashSet;
- import java.util.Iterator;
- public class HashSetDemo {
- public static void sop(Object obj){
- System.out.println(obj);
- }
- public static void main(String[] args){
- HashSet al=new HashSet();
- al.add(new Person("a1",22));
- al.add(new Person("a2",24));
- al.add(new Person("a3",25));
- al.add(new Person("a4",26));
- al.add(new Person("a5",27));
- Iterator it=al.iterator();
- while(it.hasNext()){
- Person p=(Person)it.next();
- sop(p.GetName());
- }
- }
- }
- class Person{//<font color="#ff0000"><b>提示我这里错误</b></font>
- private String name;
- private int age;
- Person(String name,int age){
- this.name=name;
- this.age=age;
- }
- public boolean equals(Object obj){
- if(!(obj instanceof Person))
- return false;
- Person p=(Person)obj;
- return this.name.equals(p.name)&&this.age==p.age;
-
- }
- public String GetName(){
- return name;
- }
- public int GetAge(){
- return age;
- }
- }
复制代码
|
|