CCB Premier Import
This commit is contained in:
30
11-SOURCES/generique/Box.java
Normal file
30
11-SOURCES/generique/Box.java
Normal file
@@ -0,0 +1,30 @@
|
||||
package generique;
|
||||
|
||||
public class Box<T> {
|
||||
private T t;
|
||||
|
||||
public void set(T t) { this.t = t; }
|
||||
public T get() { return t; }
|
||||
|
||||
public static void main (String args []) {
|
||||
// D<>claration et new avec le type
|
||||
Box<Integer> integerBox = new Box<Integer>();
|
||||
integerBox.set(new Integer (12));
|
||||
// integerBox.set("Bonjour"); // ERREUR DE COMPILATION :
|
||||
// The method set(Integer) in the type Box<Integer> is not applicable
|
||||
// for the arguments (String)
|
||||
Integer i = integerBox.get(); // PAS LA PEINE DE FAIRE UN CAST
|
||||
|
||||
// Ou alors avec le <20>diamant<6E> et une inf<6E>rence de type
|
||||
Box<Integer> integerBox2 = new Box<>();
|
||||
integerBox2.set(new Integer (33));
|
||||
Integer j = integerBox2.get();
|
||||
|
||||
// Le type <20>brut<75> : Box
|
||||
Box rawBox = new Box();
|
||||
rawBox.set(new Integer (35));
|
||||
// Integer k = rawBox.get(); ERREUR DE COMPILATION :
|
||||
// Type mismatch: cannot convert from Object to Integer
|
||||
int k = (Integer) rawBox.get();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user