1. 程式人生 > 其它 >java--泛型--型別萬用字元

java--泛型--型別萬用字元

  1. 什麼是型別萬用字元
    1. 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; } }
  2. 型別萬用字元的上限

    1. 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{
      
      }
  3. 型別萬用字元的下限
    1. 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;
      
          }
      
      }