美文网首页
泛型限定

泛型限定

作者: vv_64ce | 来源:发表于2018-12-12 19:08 被阅读0次

泛型的通配符:?

可以对类型进行限定:? extends E :接收E类型或者E的子类型对象。上限
? super E:接收E类型或者E的父类型。下限
使用同一个方法操作泛型不同的对象时:< ? >
ArrayList和LinkedList的父类是Collection,故在操作时, printCollection(Collection<?> al)应该是Collection

package com.vv.generic.advance.demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import com.vv.bean.Student;
import com.vv.bean.Worker;

public class GenericAdvanceDemo {

    public static void main(String[] args) {

        ArrayList<Worker> al = new ArrayList<Worker>();
        al.add(new Worker("worker",12));
        al.add(new Worker("worker2",32));
        
        LinkedList<Student> al2 = new LinkedList<Student>();
        al.add(new Worker("student",12));
        al.add(new Worker("student2",12));
        
        printCollection(al);
        printCollection(al2);
    }

    private static void printCollection(Collection<?> al) {
        Iterator<?> it = al.iterator();
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
    }

}

当方法时静态的时候,泛型限定也可以改为:此时it.next()就可以有返回值,但若是?形式,则不能进行it.next()

    private static <l> void printCollection(Collection<l> al) {
        Iterator<l> it = al.iterator();
        while(it.hasNext()){
            l str = it.next();
            System.out.println(it.next());
        }

如上,一般都采用第一种,只有当需要操作返回值时才需要进行使用第二种:

private static <l> l printCollection(Collection<l> al) {
        
        Iterator<l> it = al.iterator();
        l str = it.next();
        return str;
    }

如何使用泛型操作一部分,即只能存储person的子类,即泛型的限定;像LinkedList<Integer> al3 = new LinkedList<Integer>();
这种方式进行添加数据时编译会报错,因为只能存储Person的子类,

package com.vv.generic.advance.demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import com.vv.bean.Person;
import com.vv.bean.Student;
import com.vv.bean.Worker;

public class GenericAdvanceDemo {

    public static void main(String[] args) {

        ArrayList<Worker> al = new ArrayList<Worker>();
        al.add(new Worker("worker",12));
        al.add(new Worker("worker2",32));
        
        LinkedList<Student> al2 = new LinkedList<Student>();
        al.add(new Worker("student",12));
        al.add(new Worker("student2",12));
        
        LinkedList<Integer> al3 = new LinkedList<Integer>();
        al3.add(5);
        al3.add(5);
        
        printCollection(al);
        printCollection(al2);
        printCollection(al3);

    }

    private static  void  printCollection(Collection<? extends Person> al) {
        
        Iterator<? extends Person> it = al.iterator();
    
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
    }

}

上限:
如下情况,迭代器接收数据时,就可以进行使用Person父类进行数据的输出

package com.vv.generic.advance.demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import com.vv.bean.Person;
import com.vv.bean.Student;
import com.vv.bean.Worker;

public class GenericAdvanceDemo {

    public static void main(String[] args) {

        ArrayList<Worker> al = new ArrayList<Worker>();
        al.add(new Worker("worker",12));
        al.add(new Worker("worker2",32));
        
        LinkedList<Student> al2 = new LinkedList<Student>();
        al.add(new Worker("student",12));
        al.add(new Worker("student2",12));
    
        printCollection(al);
        printCollection(al2);


    }

    private static  void  printCollection(Collection<? extends Person> al) {
        
        Iterator<? extends Person> it = al.iterator();
    
        while(it.hasNext()){
            Person p = it.next();
//          System.out.println(it.next());
            System.out.println(p.getName()+p.getAge());
        }
        
    }

}

下限:
即泛型中存储时只能存储Student和它的父类

private static  void  printCollection(Collection<? super Student> al) {
        
        Iterator<? super Student> it = al.iterator();
    
        while(it.hasNext()){
            System.out.println(it.next());
        }
        
    }

一般在存储元素的时候都是使用上限,因为这样取出都是按照上限类型进行运算的,不会出现类型安全隐患


泛型上限.png

如下 ArrayList<Student> al4 = new ArrayList<Student>();
将泛型定义为Person时才可以编译成功,
上限:存什么用什么类型进行接收

package com.vv.generic.advance.demo;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import com.vv.bean.Person;
import com.vv.bean.Student;
import com.vv.bean.Worker;

public class GenericAdvanceDemo {

    public static void main(String[] args) {
        
        ArrayList<Worker> al1 = new ArrayList<Worker>();
        al1.add(new Worker("worker",12));
        al1.add(new Worker("worker2",32));
        
        LinkedList<Student> al2 = new LinkedList<Student>();
        al2.add(new Student("student",12));
        al2.add(new Student("student2",2));
        
        ArrayList<Person> al3 = new ArrayList<Person>();
        al3.add(new Person("person",11));
        al3.add(new Person("person2",21));
        
        ArrayList<Student> al4 = new ArrayList<Student>();
        al4.addAll(al1);
    }

}

下限:


泛型下限.png

通常对集合中的元素进行取出操作时,即取什么类型使用什么的父类进行接收

package com.vv.generic.advance.demo;

import java.util.Comparator;
import java.util.TreeSet;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;

import com.vv.bean.Person;
import com.vv.bean.Student;
import com.vv.bean.Worker;

public class GenericAdvanceDemo {

    public static void main(String[] args) {
        
        TreeSet<Worker> al1 = new TreeSet<Worker>(new CompByName());
        al1.add(new Worker("worker",12));
        al1.add(new Worker("worker2",32));
        
        TreeSet<Student> al2 = new TreeSet<Student>(new CompByName());
        al2.add(new Student("student",12));
        al2.add(new Student("student2",2));
        
        TreeSet<Person> al3 = new TreeSet<Person>(new CompByName());
        al3.add(new Person("person",11));
        al3.add(new Person("person2",21));
        
        TreeSet<Person> al4 = new TreeSet<Person>();
//      al4.addAll(al2);
//      al4.addAll(al1);
//      al4.addAll(al3);
//      System.out.println(al4.size());
        
        Iterator<Student> it = al2.iterator();
        
        while(it.hasNext()){
//          Person p = it.next();
            System.out.println(it.next());
//          System.out.println(p.getName()+p.getAge());
        }

    }

    
        
    }
class CompByName implements Comparator<Person>{

    @Override
    public int compare(Person o1, Person o2) {
        
        int temp = o1.getName().compareTo(o2.getName());
        
        return temp==0? o1.getAge()-o2.getAge():temp;
    }
    
}

相关文章

  • java基础

    八、泛型 面试题==什么是泛型中的限定通配符和非限定通配符 ?这是另一个非常流行的Java泛型面试题。限定通配符对...

  • 泛型机制(二)

    泛型通配符 实例代码 结果 泛型的上下限限定 定义

  • Java 泛型

    一、泛型使用 1、泛型类 2、泛型函数 二、泛型限定符 1、泛型上限 泛型上限使用 extends 限制 多个限制...

  • 泛型限定

    泛型的通配符:? 可以对类型进行限定:? extends E :接收E类型或者E的子类型对象。上限? super ...

  • Android开发之深入理解泛型extends和super的区别

    摘要: 什么是泛型?什么是擦除边界?什么是上界限定或下界限定(子类型限定或超类型限定)?什么是类型安全?泛型ext...

  • java泛型

    1:问:什么是 Java 泛型中的限定通配符和非限定通配符?有什么区别? 答:限定通配符对类型进行限制,泛型中有两...

  • Java泛型详解

    一,打破砂锅问到底 泛型存在的意义?泛型类,泛型接口,泛型方法如何定义?如何限定类型变量?泛型中使用的约束和局限性...

  • Java泛型,你都了解了吗?

    大纲一、为什么需要泛型?泛型的优点二、泛型定义三、限定"类型变量"四、泛型中的约束和局限性五、泛型类型的继承规则和...

  • 九、泛型

    九、泛型 尽量不要写这种有能与泛型方法重载的代码,有迷惑性 限定传入 T 的类型 泛型数组 Java 不支持泛型类...

  • 泛型、型变与投影

    简单泛型 kotlin 对于简单泛型的支持与java类似, 可以通过通配符,提升代码的灵活度 限定型泛型 在编码实...

网友评论

      本文标题:泛型限定

      本文链接:https://www.haomeiwen.com/subject/acvrhqtx.html