泛型的使用

泛型

泛型的概念:泛型的本质参数化类型,泛型提供了编译时类型的安全检测机制,该机制允许程序在编译时检测非法类型,比如要实现一个字符串(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);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
泛型(Generics)是一种在编程语言中实现参数化类型的技术,可以让我们编写更加灵活和通用的代码。下面是一个泛型使用案例: 假设我们有一个需求,需要实现一个通用的栈(Stack)数据结构,可以存储任何类型的元素。我们可以使用泛型来实现这个通用的栈数据结构。以下是一个基于Java的示例代码: ```java public class Stack<T> { private ArrayList<T> items; public Stack() { items = new ArrayList<T>(); } public void push(T item) { items.add(item); } public T pop() { if (items.isEmpty()) { throw new RuntimeException("Stack is empty"); } return items.remove(items.size() - 1); } public boolean isEmpty() { return items.isEmpty(); } } ``` 在上面的代码中,我们使用了一个类型参数 `T`,它代表任何类型。我们在类的定义中使用了 `<T>` 来声明这个类是一个泛型类,它可以接受任何类型的元素。在类的内部,我们使用 `T` 来代表元素的类型。我们将元素存储在一个 `ArrayList<T>` 中,这个 `ArrayList` 可以存储任何类型的元素。 我们定义了三个方法:`push()`、`pop()` 和 `isEmpty()`。`push()` 方法用于将元素压入栈中,`pop()` 方法用于弹出栈顶元素,并从栈中移除它,`isEmpty()` 方法用于判断栈是否为空。 使用泛型,我们可以使用这个通用的栈数据结构来存储任何类型的元素,例如: ```java Stack<Integer> intStack = new Stack<Integer>(); intStack.push(1); intStack.push(2); intStack.push(3); intStack.pop(); // 返回 3 intStack.pop(); // 返回 2 Stack<String> strStack = new Stack<String>(); strStack.push("Hello"); strStack.push("World"); strStack.pop(); // 返回 "World" ``` 在上面的示例代码中,我们分别使用了 `Stack<Integer>` 和 `Stack<String>` 来存储整数和字符串类型的元素。由于使用泛型,这个通用的栈数据结构可以存储任何类型的元素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值