• Java中的泛型


    泛型:是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型。

    参数化类型,把类型当做参数一样的传递。

    格式:

    <数据类型>

    此处的数据类型只能是引用类型

    好处:

    A:把运行时期的问题提前到了编译期间

    B:避免了强制类型转换

    C:优化了程序设计,解决了黄色警告线

    泛型在哪些地方使用呢?

    看API,如果类,接口,抽象类后面跟的有<E>就说明要使用,一般来说就是在集合中使用。

    //用ArrayList存储字符串元素,并遍历。用泛型改进代码

    ArrayList<String> array = new ArrayList<String>();

    array.add("hello");

    array.add("world");

    array.add("java");

    Iterator<String> it = array.iterator();

    while(it.hasNext()){

    String s = it.next();

    System.out.println(s);

    }

    for(int x=0;x<array.size();x++){

    String s = array.get(x);

    System.out.println(s);

    }

    //最后输出:hello

    world

    java

    hello

    world

    java

    //ArrayList存储自定义对象并遍历泛型版

    测试类:

    package javatest;

    import java.util.ArrayList;

    import java.util.Iterator;

    public class A {

    public static void main(String[] args) {

    // 创建集合对象

    ArrayList<Student> array = new ArrayList<Student>();

    // 创建元素对象

    Student s1 = new Student("曹操", 40);

    Student s2 = new Student("蒋干", 30);

    Student s3 = new Student("诸葛亮", 26);

    // 添加元素

    array.add(s1);

    array.add(s2);

    array.add(s3);

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

    while (it.hasNext()) {

    Student s = it.next();

    System.out.println(s.getName() + "----" + s.getAge());

    }

    for (int x = 0; x < array.size(); x++) {

    Student s = array.get(x);

    System.out.println(s);

    }

    }

    }

    //最后输出:曹操----40

    蒋干----30

    诸葛亮----26

    Student: [name=曹操,age=40]

    Student: [name=蒋干,age=30]

    Student: [name=诸葛亮,age=26]

    工具类:

    package javatest;

    public class Student {

    // 成员变量

    private String name;

    private int age;

    // 构造方法

    public Student() {

    super();

    }

    public Student(String name, int age) {

    super();

    this.name = name;

    this.age = age;

    }

    // get方法set方法

    public String getName() {

    return name;

    }

    public void setName(String name) {

    this.name = name;

    }

    public int getAge() {

    return age;

    }

    public void setAge(int age) {

    this.age = age;

    }

    public String toString() {

    return "Student: [name=" + name + ",age=" + age + "]";

    }

    }

    泛型方法:把泛型定义在方法上

    public class ObjecTool{

    public <T> void show(T t){

    System.out.println(t);

    }

    }

    public static void main(String[] args){

    //定义泛型方法后

    ObjectTool ot = new ObjectTool();

    ot.show("hello");

    ot.show(100);

    ot.show(true);

    表示 传入什么类型的值 <T> 就是什么类型的方法

    //把泛型定义在接口上

    实现类在实现接口的时候

    第一种情况:已经知道是什么类型的了(不常见)

    public class InterImpl implements Inter<String> {

    public void show(String t){

    System.out.println(t);//这个时候方法就是String类型的了

    }

    }

    第二种情况(泛型类):还不知道是什么类型的(创建的时候不知道是什么类型 就是一个<T>,当测试类调用传入的时候,是什么值对应的方法就是什么类型)

    public class InterImpl<T> implements Inter<T>{

    public void show(T t){

    System.out.println(t);

    }

    }

    //泛型高级之通配符

    <?>:任意类型,如果没有明确,那么就是Object以及任意的Java类了

    ?extend E:向下限定,E及其子类

    ?super E: 向上限定,E及其父类

    public static void main(String[] args){

    //泛型如果明确写的时候,前后必须一致

    Collection<Object> c1 = new ArrayList<Object>();

    //Collection<Object> c2 = new ArrayList<Animal>();-----报错

    //Collection<Object> c3 = new ArrayList<Dog>();-----报错

    //Collection<Object> c4 = new ArrayList<Cat>();-----报错

    //?表示任意的类型都是可以的

    Collection<?> c5 = new ArrayList<Object>();---全不报错

    Collection<?> c6 = new ArrayList<Animal>();

    Collection<?> c7 = new ArrayList<Dog>();

    Collection<?> c8 = new ArrayList<Cat>();

    //?extends E:向下限定,E及其子类:也就是说下面不报错的只能是Animal或者它(Animal)的子类,所以第一个报错

    //Collection<? extend Animal> c9  = new ArrayList<Object>();---报错

    Collection<? extend Animal> c10 = new ArrayList<Animal>();

    Collection<? extend Animal> c11 = new ArrayList<Dog>();

    Collection<? extend Animal> c12 = new ArrayList<Cat>();

    //?super E: 向上限定,E及其父类

    Collection<? super Animal> c13  = new ArrayList<Object>();

    Collection<? super Animal> c14 = new ArrayList<Animal>();

    //Collection<? super Animal> c15 = new ArrayList<Dog>();----报错

    //Collection<? super Animal> c16 = new ArrayList<Cat>();----报错

    }

  • 相关阅读:
    1523. K-inversions URAL 求k逆序对,,,,DP加树状数组
    Football 概率DP poj3071
    Collecting Bugs poj2096 概率DP
    E. Exposition
    Subsequence
    D. How many trees? DP
    hdu 1542 线段树 求矩形并
    Huge Mission
    2013 ACM/ICPC Asia Regional Chengdu Online hdu4731 Minimum palindrome
    008 jackson的一些使用记录
  • 原文地址:https://www.cnblogs.com/lszbk/p/12318353.html
Copyright © 2020-2023  润新知