Java Generics 简明教程

Java Generics - Guidelines for Wildcard Use

通配符可以用三种方式使用 −

Wildcards can be used in three ways −

  1. Upper bound Wildcard − ? extends Type.

  2. Lower bound Wildcard − ? super Type.

  3. Unbounded Wildcard − ?

为了确定哪种类型的通配符最适合该条件,首先让我们将传递给方法的类型参数分类为 inout 参数。

In order to decide which type of wildcard best suits the condition, let’s first classify the type of parameters passed to a method as in and out parameter.

  1. in variable − An in variable provides data to the code. For example, copy(src, dest). Here src acts as in variable being data to be copied.

  2. out variable − An out variable holds data updated by the code. For example, copy(src, dest). Here dest acts as in variable having copied data.

Guidelines for Wildcards.

  1. Upper bound wildcard − If a variable is of in category, use extends keyword with wildcard.

  2. Lower bound wildcard − If a variable is of out category, use super keyword with wildcard.

  3. Unbounded wildcard − If a variable can be accessed using Object class method then use an unbound wildcard.

  4. No wildcard − If code is accessing variable in both in and out category then do not use wildcards.

Example

以下示例阐明了上述概念。

Following example illustrates the above mentioned concepts.

package com.tutorialspoint;

import java.util.ArrayList;
import java.util.List;

public class GenericsTester {

   //Upper bound wildcard
   //in category
   public static void deleteCat(List<? extends Cat> catList, Cat cat) {
      catList.remove(cat);
      System.out.println("Cat Removed");
   }

   //Lower bound wildcard
   //out category
   public static void addCat(List<? super RedCat> catList) {
      catList.add(new RedCat("Red Cat"));
      System.out.println("Cat Added");
   }

   //Unbounded wildcard
   //Using Object method toString()
   public static void printAll(List<?> list) {
      for (Object item : list)
         System.out.println(item + " ");
   }

   public static void main(String[] args) {

      List<Animal> animalList= new ArrayList<Animal>();
      List<RedCat> redCatList= new ArrayList<RedCat>();

      //add list of super class Animal of Cat class
      addCat(animalList);
      //add list of Cat class
      addCat(redCatList);
      addCat(redCatList);

      //print all animals
      printAll(animalList);
      printAll(redCatList);

      Cat cat = redCatList.get(0);
      //delete cat
      deleteCat(redCatList, cat);
      printAll(redCatList);
   }
}

class Animal {
   String name;
   Animal(String name) {
      this.name = name;
   }
   public String toString() {
      return name;
   }
}

class Cat extends Animal {
   Cat(String name) {
      super(name);
   }
}

class RedCat extends Cat {
   RedCat(String name) {
      super(name);
   }
}

class Dog extends Animal {
   Dog(String name) {
      super(name);
   }
}

这会产生以下结果 −

This will produce the following result −

Cat Added
Cat Added
Cat Added
Red Cat
Red Cat
Red Cat
Cat Removed
Red Cat