java - 的意义?在泛型中

标签 java jakarta-ee generics

查看了在线截取的代码

public Collection<? extends GrantedAuthority> getAuthorities(Integer role) {
        List<GrantedAuthority> authList = getGrantedAuthorities(getRoles(role));
        return authList;
    }

A.谁能告诉我 ? 是什么意思吗?表示什么?

B. <? extends GrantedAuthority>哪个类正在扩展 GrantedAuthority?

C.在泛型中,我们在<>中提供类类型以确保类型安全,为什么有人想在<>中提供A扩展B?

最佳答案

它是一个通配符,表示任何扩展对象的类型(也包括对象)。

所以你可以说<?><? extends Object> 的简写

检查Oracle docs for Type Arguments and Wildcards

来自here :

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific). The wildcard is never used as a type argument for a generic method invocation, a generic class instance creation, or a supertype.

关于java - 的意义?在泛型中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22709444/

相关文章:

java - 如何设计一个具有许多独立流程的企业应用程序?

java - 原始类型在 Java 中如何工作?

java - 如何调用同名不同返回值的不同类型对象的方法?

java - 两个 Web 项目在 Eclipse 中共享相同的基本源代码

java - findByUsername 或 Facade 抽象类的 find 方法

c# - 通用接口(interface)中的协变

java - 未定义 [com.gemstone.gemfire.cache.GemFireCache] 类型的唯一 bean : expected single bean but found 2

java - 使用 nameFilters 排除特定类型的项目

java - 3.0 中的 Spring SimpleFormController

java - 在java中替换文本文件的内容