16 lines
418 B
Java
16 lines
418 B
Java
//: generics/Diamond.java
|
|
|
|
public class Diamond<T> {
|
|
private T a;
|
|
public Diamond(T a) { this.a = a; }
|
|
public void set(T a) { this.a = a; }
|
|
public T get() { return a; }
|
|
public static void main(String[] args) {
|
|
Diamond<Automobile> h3 =
|
|
new Diamond<>(new Automobile());
|
|
Automobile a = h3.get(); // No cast needed
|
|
// h3.set("Not an Automobile"); // Error
|
|
// h3.set(1); // Error
|
|
}
|
|
} ///:~
|