简体   繁体   English

类有什么区别<? extends T>和班级<? extends ?>在爪哇?

[英]What is the difference between Class<? extends T> and Class<? extends ?> in Java?

I was going through the some legacy code in my application and I found uses of Java generics.我在我的应用程序中浏览了一些遗留代码,我发现了 Java 泛型的用途。

Can you please tell me, what is the difference between你能告诉我,这两者之间有什么区别吗?

Class<? extends T>

and

Class<? extends ?>

I am not clear about how and when to use these generics.我不清楚如何以及何时使用这些泛型。

Class<? extends ?> Class<? extends ?> is not valid type in java. Class<? extends ?>不是 java 中的有效类型。

Class<? extends T> Class<? extends T> is valid type in java. Class<? extends T>是 java 中的有效类型。

Class<? extends T Class<? extends T > example: Class<? extends T > 示例:

Class<? extends Number> Class<? extends Number> can be Class<Number> , Class<Integer> , Class<BigDecimal> etc... In other words any Class that extends Number class. Class<? extends Number>可以是Class<Number>Class<Integer>Class<BigDecimal>等……换句话说,任何扩展Number类的类。 That is checked in compile time!那是在编译时检查的!

On the other hand Class<? extends ?>另一方面Class<? extends ?> Class<? extends ?> can be interpreted as class of type any class which extend any class and that is a nonsense. Class<? extends ?>可以被解释为扩展任何类的任何类的类型,这是无稽之谈。

EDIT: You're asking for usage.编辑:您要求使用。 Here is an example of code that uses List<? extends Number>下面是一个使用List<? extends Number> List<? extends Number> to calculate sum and average of all numbers in the list: List<? extends Number>计算列表中所有数字的总和和平均值:

public class Help {

    private static BigDecimal sum(List<? extends Number> list) {
        return list == null || list.isEmpty() ?
                BigDecimal.ZERO :
                list.stream()
                        .map(n -> new BigDecimal(n.doubleValue()))
                        .reduce(BigDecimal::add)
                        .get();
    }

    private static BigDecimal average(List<? extends Number> list) {
        return list == null || list.isEmpty() ?
                BigDecimal.ZERO :
                sum(list).divide(BigDecimal.valueOf(list.size()), MathContext.DECIMAL32);
    }

    public static void main(String[] args) {

        List<Number> numbers =
                List.of(
                        1.0D,                           /*double*/
                        2.0F,                           /*float*/
                        3L,                             /*long*/
                        new BigInteger("4"),            /*BigInteger*/
                        new AtomicInteger(5),           /*just for test*/
                        (int)'a'                        /*int*/
                );

        System.out.println("sun of " + numbers + " = " + sum(numbers));
        System.out.println("avg of " + numbers + " = " + average(numbers));
        System.out.println("sum of empty list = " + sum(List.of()));
        System.out.println("avg of empty list = " + average(List.of()));
        System.out.println("sum of null list = " + sum(null));
        System.out.println("avg of null list = " + average(null));

    }
}

I hope that you can get some basic tips from this code.我希望你能从这段代码中得到一些基本的技巧。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM