汎用的な使用の体現

11643 ワード

汎用限定_上限の体現:一般的に元素を格納する時すべて上限を使うので、このように取り出してすべて上限のタイプによって演算します.タイプのセキュリティ上の危険は発生しません.
 1 import java.util.ArrayList;

 2 

 3 import cn.itcast.p2.bean.Person;

 4 import cn.itcast.p2.bean.Student;

 5 import cn.itcast.p2.bean.Worker;

 6 

 7 public class GenericAdvanceDemo {

 8     public static void main(String[] args) {

 9 

10         ArrayList<Person> al1 = new ArrayList<Person>();

11         

12         al1.add(new Person("abc",30));

13         al1.add(new Person("abc4",34));

14         

15         ArrayList<Student> al2 = new ArrayList<Student>();

16         

17         al2.add(new Student("stu1",11));

18         al2.add(new Student("stu2",22));

19         

20         

21         ArrayList<Worker> al3 = new ArrayList<Worker>();

22         

23         al3.add(new Worker("stu1",11));

24         al3.add(new Worker("stu2",22));

25         

26         ArrayList<String> al4 = new ArrayList<String>();

27         al4.add("abcdeef");

28 //        al1.addAll(al4);//  ,     。

29         

30         al1.addAll(al2);

31         al1.addAll(al3);

32         

33         System.out.println(al1.size());

34         

35         

36 //        printCollection(al2);

37 //        printCollection(al);

38     }

39     

40     

41 

42 }

43 

44 class MyCollection<E>{

45     public void add(E e){

46     

47     }

48     public void addAll(MyCollection<? extends E> e){

49         

50     }

51 }

 
 
汎用限定_下限の体現:通常集合中の要素を取り出す操作を行う場合、下限を用いることができる.
import java.util.Comparator;

import java.util.Iterator;

import java.util.TreeSet;



import cn.itcast.p2.bean.Person;

import cn.itcast.p2.bean.Student;

import cn.itcast.p2.bean.Worker;



public class GenericAdvanceDemo4 {

    public static void main(String[] args) {



        TreeSet<Person> al1 = new TreeSet<Person>(new CompByName());

        

        al1.add(new Person("abc4",34));

        al1.add(new Person("abc1",30));

        al1.add(new Person("abc2",38));

        

        TreeSet<Student> al2 = new TreeSet<Student>(new CompByName());

        

        al2.add(new Student("stu1",11));

        al2.add(new Student("stu7",20));

        al2.add(new Student("stu2",22));

        

        

        TreeSet<Worker> al3 = new TreeSet<Worker>();

        

        al3.add(new Worker("stu1",11));

        al3.add(new Worker("stu2",22));

        

        TreeSet<String> al4 = new TreeSet<String>();

        al4.add("abcdeef");

//        al1.addAll(al4);//  ,     。

        

//        al1.addAll(al2);

//        al1.addAll(al3);

        

//        System.out.println(al1.size());

        

        

        

        Iterator<Student> it = al2.iterator();

        while(it.hasNext()){

            System.out.println(it.next());

        }

        

    }

}





/*

 * class TreeSet<Worker>

 * {

 *         Tree(Comparator<? super Worker> comp);

 * }

 * 

 *         ?                ,      。

 * 

 */



class CompByName implements Comparator<Person>{



    @Override

    public int compare(Person o1, Person o2) {

        

        int temp = o1.getName().compareTo(o2.getName());

        

        return temp==0? o1.getAge()-o2.getAge():temp;

    }

    

}



class CompByStuName implements Comparator<Student>{



    @Override

    public int compare(Student o1, Student o2) {

        

        int temp = o1.getName().compareTo(o2.getName());

        

        return temp==0? o1.getAge()-o2.getAge():temp;

    }

    

}



class CompByWorkerName implements Comparator<Worker>{



    @Override

    public int compare(Worker o1, Worker o2) {

        

        int temp = o1.getName().compareTo(o2.getName());

        

        return temp==0? o1.getAge()-o2.getAge():temp;

    }

    

}