本帖最后由 陈嘉宾 于 2012-6-3 12:14 编辑
下面public boolean equals(Object obj) 这段话中的return this.name.equals(p.name) && this.age==p.age;这个this调用的是谁?,是用谁的姓名跟年龄开始比较?
{
if(!(obj instanceof person))
return false;
person p =(person)obj;
//System.out.println(this.name"....."+p.name);
return this.name.equals(p.name) && this.age==p.age;
}
class person
{
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;
//System.out.println(this.name"....."+p.name);
return this.name.equals(p.name) && this.age==p.age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age:
}
}
class ArrayListTest2
{
public static void sop(Object obj)
{
System.out.println(obj);
}
public static void main(String[] args)
{
ArrayList al= new ArrayList();
al.add(new preson("lisi01",30));//al/add(Object obj);//Object obj=new preson("lisi",30);
al.add(new preson("lisi02",32));
al.add(new preson("lisi02",32));
al.add(new preson("lisi03",33));
al.add(new preson("lisi04",35));
al.add(new preson("lisi04",35));
Iterator it =al.iterator();
while(it.hasNext())
{
person p=(person)it.next();
sop(p.getName()+"::"+p.getAge());
}
}
public static ArrayList singleElement(ArrayList al)
{
ArrayList newAl= new ArrayList();
Iterator it= al.iterator();
while(it.hasNext())
{
Object obj=it.next();
if(!newAl.contains(obj))
newAl.add(obj);
}
return newAl;
}
} |