泛型限定

package Demo;
import java.util.*;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

public class Demo2 {

    public static void main(String[] args) {
        ArrayList<person> al=new ArrayList<person>();
        al.add(new person("aa1"));
        al.add(new person("aa3"));
        al.add(new person("aa2"));
        method(al);
        ArrayList<student> s=new ArrayList<student>();
        s.add(new student("ss1"));
        s.add(new student("ss2"));
        s.add(new student("ssscg"));
        method(s);
    }
    public static  void method(ArrayList<? extends person> al)
    {
        Iterator<? extends person>  it=al.iterator();
        while(it.hasNext())
        {
            person p=(person)it.next();
            String name=p.getname();
            System.out.println(name);
        }
    }
}
class person
{
    private String name;
    person(String name)
    {
        this.name=name;
    }
    public String getname()
    {
        return name;
    }
}
class student extends person
{
    student(String name)
    {
        super(name);
    }
}

posted on 2012-10-19 17:16  蝌蚪的精神  阅读(146)  评论(0编辑  收藏  举报

导航