泛型

68 阅读3分钟

泛型

泛型的概念:泛型的本质参数化类型,泛型提供了编译时类型的安全检测机制,该机制允许程序在编译时检测非法类型,比如要实现一个字符串(String) ,整形(int),浮点型(Float),对象(object)进行大小比较的方法,就可以使用java泛型。

1.1泛型标记和泛型限定:E,T,K,V,N,?

在使用泛型前要了解有哪些泛型标记; 1.E-Element 在集合在使用表示在集合中存放元素 2.T-Type 表示java类,包括基本的类和我们自定义的类 3.K-Key 表示键,比如map和kep 4.v-Value 表示值 5.N-Number 表示数值类型 6.? 表示不确定的java类型 类型通配符的使用'?'表示所有具体的参数类型,例如在List<?>就是List<具体的类型实参>的父类; 上界通配符 < ? extends E> 上届:用 extends 关键字声明,表示参数化的类型可能是所指定的类型,或者是此类型的子类。 下界通配符 < ? super E> 下界: 用 super 进行声明,表示参数化的类型可能是所指定的类型,或者是此类型的父类型,直至 Object

1.2泛型方法

泛型方法指将方法的参数类型定义为泛型以便在调用的时候接受不同类型的参数。在方法内部根据传递给泛型方法的不同参数类型执行不同的处理方法,具体用法如下:

//方法
  public static <T> void generalMethod(T ...inputArray){
        for (T element :inputArray){
            if (element instanceof Integer){
                System.out.println("处理integer类型数据");
            }else if (element instanceof String){
                System.out.println("处理string型数据");
            }else if (element instanceof Double){
                System.out.println("处理Double型数据");
            }else if (element instanceof Float){
                System.out.println("处理floa型数据");
            }else if (element instanceof Long){
                System.out.println("处理long型数据");
            }else if (element instanceof Boolean){
                System.out.println("处理Boolean型数据");
            }else if (element instanceof Date){
                System.out.println("处理date型数据");
            }else if (element instanceof Worker){
                System.out.println("处理Worker型数据");
            }
        }
    }
    //测试类
      public static void main(String[] args) {
        generalMethod ("1", 2 , new Worker() 
        }
        //结果
        处理string型数据
        处理integer类型数据
        处理Worker型数据

该方法是定义的一个泛型方法可也根据不同的的值返回不同的结果

1.3 泛型类

泛型类指在定义类时在类上定义了泛型,以便类在使用的时可以根据传入的不同参数类型实例化不同对象。

public class GeneralClass {
        public static void main(String[] args) {
            ArrayList<Integer> list = new ArrayList<Integer>(2);
            list.add(1);
            System.out.println(list.get(0));

        }

    }
    //E是指我们的数据类型,当我们在实例化的时候必须要指明他的类型
    class ArrayList<E>{
        private Object[] elementDate;
        private int size = 0;
        //initialCapacity初始容量
        public ArrayList(int initialCapacity){
            this.elementDate = new Object[initialCapacity];
        }
        //这里的E就是我们要添加数据的类型,其必须与最开始定义的类型相同
        public boolean add(E e){
            elementDate[size++] = e;
            return true;
        }

        public E get(int index){
            return (E) elementDate[index];
        }
    }

1.4 泛型接口

泛型接口的声明和泛型类的声明类似,通过接口名后面添加类型参数的声明部分来实现;泛型接口的具体类型一般在实现类中进行声明,不同类型的实现类处理不同的业务逻辑;

//接口
public interface IGenEral<T> {
        public T getId () ;
}
//实现类
public class GeneralIntImpl implements IGenEral<Integer>{
    @Override
    public Integer getId() {
        Random random = new Random (100 );
        return random .nextInt() ;

    }

    public static void main(String[] args) {
        GeneralIntImpl gen=new GeneralIntImpl();
        System.out.println("gen = " + gen.getId);
    }
}