class People {
private String name ;
private int age ;
private String sex ;
public People(){}
public People(String name , int age , String sex){
this.name = name ;
this.age = age;
this.sex= sex ;
}
public void setName (String name ){
this.name = name ;
}
public void setAge (int age){
this.age = age ;
}
public void setSex(String sex){
this.age = age ;
}
public String getName(){
return this.name;
}
public int getAge()
{
return this.age;
}
public String getSex() {
return this.sex ;
}
}
interface Filter {
public boolean filter(People people);
}
class MyFilter implements Filter {
public boolean filter(People people){
return people.getSex().equals("female") && people.getAge() > 20;
}
}
class HongNiang {
private People[] p ;
public HongNiang(){
p = new People[]{
new People("zhangSan", 23, "male"),
new People("liSi", 21, "female"),
new People("wangWu", 9, "male"),
new People("zhaoLiu", 26, "female"),
new People("tianQi", 84, "male"),
new People("jianBa", 23, "female"),
new People("heJiu", 59, "male"),
new People("biShi", 27, "female"),
new People("xiaoQiang", 12, "male"),
new People("wangCai", 25, "female")
};
}
public People[] getPeople(Filter filter){
People[] xiHuan = new People[5];
int cnt = 0 ;
for (int i = 0; i < p.length ; i++) {
if(filter.filter(p[i])){
xiHuan[cnt] = p[i];
cnt++ ;
}
if (cnt == 5) {
break ;
}
}
return xiHuan ;
}
}
class MainApp {
public static void main(String[] args){
HongNiang hn = new HongNiang();
People[] p = hn.getPeople(new MyFilter());
for (int i = 0; i < p.length; i++) {
if (p[i] != null) {
System.out.println(p[i]);
}
}
}
}
|