package cn.itcast;
import java.util.ArrayList;
import java.util.Iterator;
class personn
{
private String name;
private int age;
personn(String name,int age)
{
this.name=name;
this.age=age;
}
public boolean equals(Object obj)
{
if(!(obj instanceof personn))
return false;
personn p=(personn)obj;
return this.name.equals(p.name)&&this.age==p.age;
}
//--------gs--------------
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public class Demo6 {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList arr=new ArrayList();
arr.add(new personn("lishan",23));
arr.add(new personn("lishi",23));
arr.add(new personn("liwu",25));
arr.add(new personn("lishan",23));
arr.add(new personn("lishi",23));
arr.add(new personn("liliu",34));
arr.add(new personn("liqi",100));
Quc q=new Quc();
arr=q.show(arr);
//System.out.println(arr);
Iterator it=arr.iterator();
while(it.hasNext())
{
personn pp=(personn)it.next();
System.out.println("姓名:"+pp.getName()+" 年龄:"+pp.getAge());
}
}
}
class Quc
{
private ArrayList li;
Quc()
{
li=new ArrayList();
}
public ArrayList show(ArrayList st)
{
Iterator it=st.iterator();
while(it.hasNext())
{
Object obj=it.next();
if(!li.contains(obj))
{
li.add(obj);
}
}
return li;
}
}
请问他是怎么调用personn中的equals的!为什么定义在personn中!!!要详解 |
|