欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 汽车 > 时评 > Java包装类——异于c++

Java包装类——异于c++

2024/10/25 3:24:30 来源:https://blog.csdn.net/Error_Fe/article/details/140758987  浏览:    关键词:Java包装类——异于c++

在 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 的泛型和包装类,提高代码的灵活性、可重用性和类型安全性。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com