Java Tutorial/Generics/Generic Parameters

Материал из Java эксперт
Версия от 17:44, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Constraints on a Wildcard

public static void saveAll(LinkedList<? extends java.io.Serializable> list) {
    
   }
   public static void analyze(LinkedList<? super MyClass> list) {
    
   }





Multiple Type Parameters

class Pair<KeyType, ValueType> {
  // Constructor
  public Pair(KeyType aKey, ValueType aValue) {
    key = aKey;
    value = aValue;
  }
  // Get the key for this pair
  public KeyType getKey() {
    return key;
  }
  // Get the value for this pair
  public ValueType getValue() {
    return value;
  }
  // Set the value for this pair
  public void setValue(ValueType aValue) {
    value = aValue;
  }
  private KeyType key;
  private ValueType value;
}
public class MainClass {
  public static void main(String[] a) {
    Pair<Integer, String> p = new Pair<Integer, String>(1, "A");
    System.out.println(p.getKey().getClass().getName());
  }
}



java.lang.Integer


Type Parameter Bounds

import static java.lang.Math.random;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class MainClass {
  public static void main(String[] args) throws Exception {
    LinkedList<Integer> numbers = new LinkedList<Integer>();
    for (int i = 0; i < 10; i++) {
      numbers.addItem(1 + (int) (100.0 * random()));
    }
    System.out.println("\nnumbers list contains:");
    listAll(numbers);
    String filename = "C:/Numbers.bin";
    ObjectOutputStream objOut = new ObjectOutputStream(new FileOutputStream(filename));
    objOut.writeObject(numbers);
    objOut.close();
    LinkedList<Integer> values = null;
    ObjectInputStream objIn = new ObjectInputStream(new FileInputStream(filename));
    values = (LinkedList<Integer>) (objIn.readObject());
    objIn.close();
    System.out.println("\nvalues list contains:");
    listAll(values);
  }
  static void listAll(LinkedList<Integer> list) {
    Integer number = list.getFirst();
    int count = 0;
    do {
      System.out.printf("%5d", number);
      if (++count % 5 == 0) {
        System.out.println();
      }
    } while ((number = list.getNext()) != null);
  }
}



numbers list contains:
   39   63   34   47   86
   88   21   39   40    1
values list contains:
   39   63   34   47   86
   88   21   39   40    1


Using Bounded Wildcards in Methods

The syntax for using an upper bound is as follows:



GenericType<? extends upperBoundType>



111.0
18.0


Using the ? wildcard

import java.util.ArrayList;
import java.util.List;
public class MainClass {
    public static void printList (List<?> list) {
        for (Object element : list) {
            System.out.println(element);
        }
    }
    public static void main(String[] args) {
        List<String> list1 = new ArrayList<String>();
        list1.add ("Hello");
        list1.add ("World");
        printList (list1);
        List<Integer> list2 = new ArrayList<Integer>();
        list2.add(100);
        list2.add(200);
        printList(list2);
    }
}



Hello
World
100
200