Параметризированные типы в Java – Generics
public class Box<T> {
private T value;
public void setValue(T value) {
this.value = value;
}
public T getValue() {
return value;
}
public static void main(String[] args) {
Box<Integer> integerBox = new Box<>();
integerBox.setValue(10);
System.out.println("Значение integerBox: " + integerBox.getValue());
Box<String> stringBox = new Box<>();
stringBox.setValue("Привет, мир!");
System.out.println("Значение stringBox: " + stringBox.getValue());
}
}Last updated