泛型的作用
    在编译器防止将错误的类型放入到容器中

    1. /**
    2. 留意在类中计数的处理
    3. */
    4. //对apple进行计数
    5. class Apple{
    6. //所有Apple实例的总数
    7. private static long count ;//1,通过static变量来计数
    8. //id 是每个实例的编号
    9. private final long id = count++;//2,将实例的编号与类成员变量关联
    10. public long getId(){
    11. return id;
    12. }
    13. }
    14. class Orange{}
    15. public class AppleAndOrangeWithoutGeneric {
    16. public static void main(String[] args) {
    17. ArrayList arrayList = new ArrayList();
    18. for(int i = 0;i < 3;i++){
    19. arrayList.add(new Apple());
    20. }
    21. arrayList.add(new Orange());
    22. for (int i = 0 ;i< arrayList.size();i++){
    23. //System.out.println("每个苹果的编号是:"+((Apple)arrayList.get(i)).getId());
    24. }
    25. System.out.println(arrayList);
    26. }
    27. //面向接口编程的测试
    28. public static void testInterCode(){
    29. List<Apple> list = new LinkedList<>();
    30. //刚开始插入的时候 使用LinkedList来实现 插入快
    31. for(int i = 0;i < 3;i++){
    32. list.add(new Apple());
    33. }
    34. System.out.println(list);
    35. //获取数据的时候 转换为ArrayList 查询快
    36. list = new ArrayList<>();
    37. }
    38. }