====== Παραμετρικοί τύποι δεδομένων με πολλές παραμέτρους ======
Δείτε το παρακάτω παράδειγμα, όπου έχουμε ένα interface και μία κλάση με δύο παραμέτρους.
public interface Pair {
public K getKey();
public V getValue();
public void setKey(K key);
public void setValue(V value);
}
public class OrderedPair implements Pair {
private K key;
private V value;
public OrderedPair(K key, V value) {
this.key = key;
this.value = value;
}
public void setKey(K key) { this.key = key; }
public void setValue(V value) { this.value = value; }
public K getKey() { return key; }
public V getValue() { return value; }
}
Με βάση τον παραπάνω κώδικα μπορείτε να δημιουργήσετε αντικείμενα επιμέρους τύπων ως εξής:
public class OrderedPairUsage {
public static void main(String args[]) {
Pair p1 = new OrderedPair("Even", 8);
Pair p2 = new OrderedPair("hello", "world");
OrderedPair> p = new OrderedPair<>("primes", new Box());
// the following is not allowed
Pair p1 = new OrderedPair<>("hello", "world");
}
}
|Προηγούμενο: [[:java:generic_interfaces | Interfaces ως παραμετρικοί τύποι δεδομένων ]] | [[:toc | Περιεχόμενα ]] | Επόμενο: [[:java:generics_raw | Απλοί παραμετρικοί τύποι δεδομένων (Raw Generic Types) ]] |