java--泛型--类型通配符
时间:2021-07-01 17:18:32
收藏:0
阅读:0
- 什么是类型通配符
-
package com.model.fanxing; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/6/30 23:46 * 演示类型通配符 */ public class FanXingDemo07 { public static void main(String[] args) { Boss<String> boss = new Boss<>(); boss.setName("张紫韩"); get(boss); Boss<Integer> boss1 = new Boss<>(); boss1.setName(100); get(boss1); /** * 当泛型类做形参时,必须为泛型类指定具体的类型,而指定具体类型后,我们只当传入一种类型的参数,则不能实现多种类型的调用 * 即:public static void get(Boss<String> boss){ 当泛型类对象做参数时,必须指定具体的类型,(不能用E,T代替) * 指定了String类型的Boss泛型类, * 我们只能传入String的类型的泛型类的参数,当我们想传入Integer类型的泛型参数时,是错误的 * 为解决这一问题引入了 泛型通配符 ? :即可以传入任意具体类型的泛型类对象 * */ } // public static void get(Boss<String> boss){ // System.out.println(boss.getName()); // } public static void get(Boss<?> boss){ System.out.println(boss.getName()); } } class Boss<E>{ private E name; public void setName(E name) { this.name = name; } public E getName() { return name; } }
-
-
类型通配符的上限
-
package com.model.fanxing; import java.util.ArrayList; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/1 10:45 */ public class FanXingDemo08 { public static void main(String[] args) { // 1.上限测试 ArrayList<One> list1 = new ArrayList<>(); ArrayList<Two> list2 = new ArrayList<>(); ArrayList<Three> list3 = new ArrayList<>(); list1.add(new One()); list1.add(new One()); list1.add(new One()); list1.add(new One()); list2.add(new Two()); list2.add(new Two()); list2.add(new Two()); list2.add(new Two()); list3.add(new Three()); list3.add(new Three()); list3.add(new Three()); list3.add(new Three()); // testOne(list1); testOne(list2); testOne(list3); // boss1 不可以,boss2和boss3可以当作参数传入 Boss<One> boss1 = new Boss<>(); Boss<Two> boss2 = new Boss<>(); Boss<Three> boss3 = new Boss<>(); // testOne(boss1); testOne(boss2); testOne(boss3); // 2.下限测试,Boss1可以因为Boss1中传入的泛型标识类是Boss2中传入的父类 testTwo(boss1); testTwo(boss2); // testTwo(boss3); } // 上限测试,传入的参数又要求,传入泛型的参数必须是继承自Two类 public static void testOne(ArrayList<? extends Two> list){ } public static void testOne(Boss<? extends Two> boss){ //只有当创建Boss类时 // 传入的是Two类或者是Two类的子类才能当作参数传入此方法 } // 下限测试,传入泛型类的泛型标识类,必须是two类的父类 public static void testTwo(Boss<? super Two> boss){ //要求人Boss泛型类的换入的参数是 Two的父类后者是Two本身才能传入 // 当作参数 } } class One{ } class Two extends One{ } class Three extends Two{ }
-
- 类型通配符的下限
-
package com.model.fanxing; import java.util.ArrayList; import java.util.Comparator; import java.util.TreeSet; /** * @Description:测试类 * @Author: 张紫韩 * @Crete 2021/7/1 11:16 */ public class FanXingDemo09 { public static void main(String[] args) { /** * public TreeSet(Comparator<? super E> comparator) { * this(new TreeMap<>(comparator)); * } * */ //已经知道,Tree的构造哦方法可以传入Comparator<? super E>,传入Comparator接口 Comparator1 comparator1 = new Comparator1(); Comparator2 comparator2 = new Comparator2(); Comparator3 comparator3 = new Comparator3(); TreeSet<B> treeSet1 = new TreeSet<>(comparator1); //按名字比较 TreeSet<B> treeSet2 = new TreeSet<>(comparator2); //按年领比较 // TreeSet<B> treeSet3 = new TreeSet<>(comparator3); //传入的比较器泛型类型不能是B类的子类,所以不能按照Comparator3比器进行比较 // TreeSet传入的泛型标识是B,而Comparator3比较传入的C并非是B类的父类 treeSet1.add(new B("a", 18)); treeSet1.add(new B("b", 19)); treeSet1.add(new B("c", 20)); treeSet1.add(new B("d", 10)); for (B tree:treeSet1){ System.out.println(tree.toString()); } treeSet2.add(new B("a", 18)); treeSet2.add(new B("b", 19)); treeSet2.add(new B("c", 20)); treeSet2.add(new B("d", 10)); for (B tree:treeSet2){ System.out.println(tree.toString()); } } public static void show(TreeSet<? super B> treeSet){ // 下限 约束可以添加元素,且只能添加B和B的子类不能添加B的父类 treeSet.add(new C("a", 18, "男")); // treeSet.add(new A("a")); treeSet.add(new B("a", 18)); } } //定义Comparator接口实现类 class Comparator1 implements Comparator<A> { @Override public int compare(A o1, A o2) { return o1.getName().compareTo(o2.getName()); } } class Comparator2 implements Comparator<B> { @Override public int compare(B o1, B o2) { return o1.getAge().compareTo(o2.getAge()); //只能引用类型的,不能比较基本数据类型 } } class Comparator3 implements Comparator<C> { @Override public int compare(C o1,C o2) { return o1.getSex().compareTo(o2.getSex()); } } class A{ public String name; public String getName() { return name; } public void setName(String name) { this.name = name; } public A(String name) { this.name = name; } @Override public String toString() { return "A{" + "name=‘" + name + ‘\‘‘ + ‘}‘; } } class B extends A{ private Integer age; public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public B(String name, Integer age) { super(name); this.age = age; } @Override public String toString() { return "B{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ‘}‘; } } class C extends B{ private String sex; public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } @Override public String toString() { return "C{" + "sex=‘" + sex + ‘\‘‘ + ‘}‘; } public C(String name, int age, String sex) { super(name, age); this.sex = sex; } }
-
评论(0)