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()); // true

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

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值