Ce document est mis à disposition selon les termes de la Licence Creative Commons Attribution - Pas d’Utilisation Commerciale - Partage dans les Mêmes Conditions 4.0 International.
2. Compréhension
Dans cette section, pour chaque question, la procédure est de :
-
Déterminer si le code compile
-
Vérifier dans l’AGL
-
Indiquer la raison pour laquelle le code compile (ou pas)
ArrayList<Integer> integers = new ArrayList<Integer>(); (1)
List<Integer> integers = new ArrayList<>(); (2)
List<? extends Integer> integers = new ArrayList<>(); (3)
List<? extends Integer> integers = new ArrayList<?>(); (4)
List<? extends Integer> integers = new ArrayList<>(); (5)
integers.add(1);
List<? extends Integer> integers = Arrays.asList(1, 2, 3); (6)
integers.get(1);
List<? super Integer> numbers = new ArrayList<>(); (7)
numbers.add(1);
List<? super Integer> numbers = Arrays.asList(1, 2, 3); (8)
numbers.get(1);
public class Generic<T> {} (9)
public class Generic<T> { (10)
public Generic(T t) {}
}
public class Generic<T> { (11)
public Generic(T t) {}
public static void main(String... args) {
Generic g = new Generic(new Object());
}
}
public class Generic<T> { (12)
public Generic(T t) {}
public static void main(String... args) {
Generic g = new Generic<>(new Object());
}
}
public class Generic<T> { (13)
public Generic(T t) {}
public static void main(String... args) {
Generic<?> g = new Generic<>(new Object());
}
}
public class Generic<T> { (14)
private final T t;
public Generic(T t) {
this.t = t;
}
public static void main(String... args) {
Generic<?> g = new Generic<>(new Object());
? t = g.t;
}
}
public class Generic<T> { (15)
private final T t;
public Generic(T t) {
this.t = t;
}
public static void main(String... args) {
Generic<?> g = new Generic<>(new Object());
Object t = g.t;
}
}
public class Generic<T> { (16)
private final T t;
public Generic(T t) {
this.t = t;
}
public static void main(String... args) {
Generic<Object> g = new Generic<>(new Object());
Object t = g.t;
}
}
public class Generic<T> { (17)
private final T t;
public Generic(T t) {
this.t = t;
}
public static void main(String... args) {
Generic<? extends Number> g = new Generic<>(1);
Object t = g.t;
}
}
public class Generic<T> { (18)
private final T t;
public Generic(T t) {
this.t = t;
}
public static void main(String... args) {
Generic<? extends Number> g = new Generic<>(1);
Number t = g.t;
}
}
public class Generic<T> { (19)
private final T t;
public Generic(T t) {
this.t = t;
}
public static void main(String... args) {
Generic<? extends Number> g = new Generic<>(1);
Integer t = g.t;
}
}
public class Generic<T> { (20)
private final T t;
public Generic(T t) {
this.t = t;
}
public static void main(String... args) {
Generic<? super Number> g = new Generic<>(1);
Number t = g.t;
}
}
public class Generic<T> { (21)
private T t;
public static void main(String... args) {
Generic<? super Number> g = new Generic<>();
g.t = 1;
}
}
public class Generic<T> { (22)
public static class GenericChild<T> extends Generic<T> {}
}
public class Generic<T> { (23)
public static class GenericChild extends Generic<Number> {}
}
public class Generic<T> { (24)
public static class GenericChild<V> extends Generic<Number> {}
}
public class NotGeneric { (25)
public void apply(T t) {}
}
public class NotGeneric { (26)
public <T> void apply(T t) {}
}
public class NotGeneric { (27)
public <V extends T> void apply(T t) {}
}
public class NotGeneric { (28)
public <T extends V> void apply(T t) {}
}
public class NotGeneric { (29)
public <T extends Number> void apply(T t) {}
}
public class NotGeneric { (30)
public <T> R apply(T t) {
return (R) new Object();
}
}
public class NotGeneric { (31)
public <T, R> R apply(T t) {
return (R) new Object();
}
}
public class NotGeneric { (32)
public <T, R extends Number> R apply(T t) {
return (R) new Object();
}
}
public class NotGeneric { (33)
public <T, R super Number> R apply(T t) {
return (R) new Object();
}
}
public class NotGeneric { (34)
public <T, R extends Number> R apply(T t) {
return (R) Integer.valueOf(1);
}
}
public class LastButNotLeast<T,V> { (35)
public void apply<T>() {}
public void apply<V>() {}
}
3. Conception
-
En utilisant les génériques, développer une méthode unique qui permet d’additionner deux nombres de n’importe quel type primitif (
byte
,int
,long
,float
etdouble
) et qui retourne undouble
. -
Soit le diagramme de classes suivant :
Modifier la conception pour faire en sorte qu’il soit possible de récupérer l’object stocké sans qu’il soit nécessaire de transtyper. Par exemple :
Triple triple = new Triple("Hello", new Date(), 15); String s = triple.first(); Date d = triple.second(); Integer i = triple.third();
-
Soit le diagramme de classes suivant :
Améliorer la conception pour :
-
Qu’il soit possible de compiler
MyString.append(MyString myString)
etMyList.append(MyList myList)
-
Qu’il ne soit pas possible de compiler
MyString.append(MyList myList)
etMyList.append(MyString myString)
Implémenter le code et vérifier
-