Generic classes in Java
Forums:
class Size<T> {
public T width;
public T height;
public Size(T width, T height) {
this.width = width;
this.height = height;
}
public String asText() {
return String.format("[%s; %s]", width, height);
}
}
Size<Integer> sizeInt = new Size<>(5, 8);
String textInt = sizeInt.asText();
//textInt is "[5; 8]"
Size<Float> sizeFloat = new Size<>(3.7f, 1.58f);
String textFloat = sizeFloat.asText();
//textFloat is "[3.7; 1.58]"
- Log in to post comments