泛型的使用

由于添加元素的集合的不确定,我可以添加任意类型的元素,那么在遍历的时候可能会出现问题。
我们之前学习过的数组,就可以声明一种类型,使得在添加数据时,数据类型不匹配的情况下,就会出现异常。
那么集合中有没有这样的方法呢?
有的,泛型。

泛型的概述

泛型是一种特殊的类型,可以理解为参数化类型,也就是可以把类型像参数一样传递。

泛型的格式:

<泛型类型>

泛型的用处

一般多用于集合。

泛型的好处

1、提高了程序的安全性,
2、将原本运行时期的错误在编译时期就暴露了出来。
3、省去了强制类型转换的麻烦。

package org.wdzl.unit07;

import java.util.ArrayList;
import java.util.Iterator;

public class StudentDemo {
    public static void main(String[] args) {
        ArrayList<Student> arrayList = new ArrayList<Student>();
        Student student1 = new Student("张三",18);
        Student student2 = new Student("刘备",22);
        Student student3 = new Student("关羽",19);
        arrayList.add(student1);
        arrayList.add(student2);
        arrayList.add(student3);
        Iterator<Student> iterator = arrayList.iterator();
        while (iterator.hasNext()){
            Student student = iterator.next();
            System.out.println(student.getName()+"------"+student.getAge());
        }
    }
}

泛型的应用

泛型类

把泛型定义在类上
泛型类型必须是引用类型。

package org.wdzl.unit07;

/**
 * 泛型类:
 *    概述:把泛型定义在类上。
 *    格式:public class 类名<泛型类型1,……>  这里的泛型类型可以有多个
 *    注意:泛型类必须时引用类型,而且要符合我们的命名规范。
 */
public class FanXIng<T> {
    private T obj;

    public T getObj() {
        return obj;
    }

    public void setObj(T obj) {
        this.obj = obj;
    }
}

package org.wdzl.unit07;

public class FxDemo {
    public static void main(String[] args) {
        FanXIng<String> fanXIng = new FanXIng<String>();
        fanXIng.setObj("郭富城");
        String name = fanXIng.getObj();
        System.out.println(name);
    }
}

泛型方法

把泛型定义在方法上
public <泛型类型> 返回值类型 方法名(泛型类型)

package org.wdzl.unit07;

/**
 * 泛型方法
 *    概述:将泛型定义在方法上。
 *    格式:public <泛型类型> 返回类型 方法名(泛型类型)
 */
public class Fx1 {
    public <T> void show(T t){
        System.out.println(t);
    }
}

package org.wdzl.unit07;

public class Fx1Demo {
    public static void main(String[] args) {
        Fx1 fx1 = new Fx1();
        fx1.show("hello");
        fx1.show(100);
        fx1.show(true);

    }
}

泛型接口

把泛型定义在接口上
public interface 接口名<泛型类型>

package org.wdzl.unit07;

/**
 * 泛型接口:
 *     概述:把泛型定义在接口上。
 * 格式:
 *    public interface 接口名<泛型类型>
 */
public interface Fx2<T> {
    public abstract void show(T t);
}

package org.wdzl.unit07;

/**
 * 在使用泛型接口时,需要明确到底什么时候明确泛型类型?
 *    1、在实现类的时候就已经明确了泛型类型
 *    2、使用的时候才知道泛型类型到底是什么。
 */
//方式1
//public class Fx2InterImpl implements Fx2<String>{
//
//    @Override
//    public void show(String o) {
//        System.out.println(o);
//    }
//}
//方式2
public class Fx2InterImpl<T> implements Fx2<T>{
    @Override
    public void show(T t) {
        System.out.println(t);
    }
}
package org.wdzl.unit07;

public class Fx2Demo {
    public static void main(String[] args) {
//        Fx2<String> fx2 = new Fx2InterImpl();
//        fx2.show("java");
        Fx2InterImpl<String> fx2Inter = new Fx2InterImpl<>();
        fx2Inter.show("Java");
        Fx2InterImpl<Integer> fx2Inter1 = new Fx2InterImpl<>();
        fx2Inter1.show(2);
    }
}

泛型的高级应用

泛型的高级应用:
1、当我们声明了一个具体的泛型类型时,我们传递的泛型只能是具体的。
2、泛型的通配符<?>
任意类型的泛型都是可以的。
3、<? extends E>
向上限定,只能是E及其子类。
4、<? super E>
向下限定,只能是E及其父类

package org.wdzl.unit07.superfx;

import java.util.ArrayList;
import java.util.Collection;

/**
 * 泛型高级应用
 */
public class AnimalDemo {
    public static void main(String[] args) {
//        Collection<?> c1 = new ArrayList<Animal>();
//        Collection<?> c2 = new ArrayList<Object>();
//        Collection<?> c3 = new ArrayList<Animal>();
//        Collection<?> c4 = new ArrayList<Cat>();

//        Collection<? extends Animal> c1 = new ArrayList<Animal>();
//        Collection<? extends Animal> c2 = new ArrayList<Object>();
//        Collection<? extends Animal> c3 = new ArrayList<Animal>();
//        Collection<? extends Animal> c4 = new ArrayList<Cat>();

//        Collection<? super Animal> c1 = new ArrayList<Animal>();
//        Collection<? super Animal> c2 = new ArrayList<Object>();
//        Collection<? super Animal> c3 = new ArrayList<Dog>();
//        Collection<? super Animal> c4 = new ArrayList<Cat>();
    }
}

  • 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、付费专栏及课程。

余额充值