在 Java 中,使用泛型和包装类的高级用法可以提高代码的可重用性、类型安全性和灵活性。以下是一些高级用法和技巧,它们可以帮助你在实际开发中更有效地利用泛型和包装类:
1. 泛型方法
除了泛型类,你还可以定义泛型方法。泛型方法允许你在方法级别使用类型参数,使方法更通用。
public class Utils {public static <T> void printArray(T[] array) {for (T element : array) {System.out.print(element + " ");}System.out.println();}
}public class Main {public static void main(String[] args) {Integer[] intArray = {1, 2, 3, 4, 5};String[] strArray = {"A", "B", "C", "D"};Utils.printArray(intArray);Utils.printArray(strArray);}
}
2. 通配符
通配符(Wildcard)允许你在泛型中使用未知类型,提供了更大的灵活性。常用的通配符有 ? extends T
和 ? super T
。
import java.util.ArrayList;
import java.util.List;public class Utils {public static void printList(List<?> list) {for (Object element : list) {System.out.print(element + " ");}System.out.println();}public static void addNumber(List<? super Integer> list) {list.add(42);}
}public class Main {public static void main(String[] args) {List<Integer> intList = new ArrayList<>();intList.add(1);intList.add(2);List<String> strList = new ArrayList<>();strList.add("A");strList.add("B");Utils.printList(intList);Utils.printList(strList);Utils.addNumber(intList);Utils.printList(intList);}
}
3. 泛型约束
你可以使用 extends
关键字来限制泛型的类型参数,使其必须是某个类的子类或实现某个接口。
class Box<T extends Number> {private T value;public void setValue(T value) {this.value = value;}public T getValue() {return value;}public double getDoubleValue() {return value.doubleValue();}
}public class Main {public static void main(String[] args) {Box<Integer> intBox = new Box<>();intBox.setValue(10);System.out.println(intBox.getDoubleValue());Box<Double> doubleBox = new Box<>();doubleBox.setValue(3.14);System.out.println(doubleBox.getDoubleValue());// Box<String> strBox = new Box<>(); // 编译错误:String 不是 Number 的子类}
}
4. 类型擦除和反射
Java 的泛型在编译时会进行类型擦除,这意味着类型参数会被替换为其上限(通常是 Object
)。可以通过反射获取类型信息。
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;class Box<T> {private T value;public void setValue(T value) {this.value = value;}public T getValue() {return value;}
}public class Main {public static void main(String[] args) {Box<Integer> intBox = new Box<>();Box<String> strBox = new Box<>();System.out.println(intBox.getClass().getName());System.out.println(strBox.getClass().getName());System.out.println(intBox.getClass() == strBox.getClass()); // trueType type = intBox.getClass().getGenericSuperclass();if (type instanceof ParameterizedType) {ParameterizedType pType = (ParameterizedType) type;System.out.println(pType.getActualTypeArguments()[0].getTypeName());}}
}
5. 使用泛型工厂方法
泛型工厂方法可以用于创建泛型对象,使代码更具灵活性和可扩展性。
class Box<T> {private T value;public void setValue(T value) {this.value = value;}public T getValue() {return value;}public static <T> Box<T> createBox(T value) {Box<T> box = new Box<>();box.setValue(value);return box;}
}public class Main {public static void main(String[] args) {Box<Integer> intBox = Box.createBox(10);Box<String> strBox = Box.createBox("Hello");System.out.println(intBox.getValue());System.out.println(strBox.getValue());}
}
通过这些高级用法和技巧,你可以更高效地利用 Java 的泛型和包装类,提高代码的灵活性、可重用性和类型安全性。