Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何减少通用<;?扩展类型>;输入Java。代码在运行时有效,但无法编译_Java_Generics - Fatal编程技术网

如何减少通用<;?扩展类型>;输入Java。代码在运行时有效,但无法编译

如何减少通用<;?扩展类型>;输入Java。代码在运行时有效,但无法编译,java,generics,Java,Generics,我使用SpringBoot.yaml进行配置,并创建了配置类的层次结构,以便其他应用程序可以定义其他属性。配置本身具有层次结构 为了保持它的扩展性,我使用了方法getsubsubclefs()的问题是subclef.conf类型为conf,假设您有一个Map和一个Map。你能在前者中插入一个子文件夹吗?后者?那么SubSubConf呢?如果您仔细考虑过,那么原因应该非常清楚。@MattiaDinosaur For泛型只在编译时存在。这并不意味着忽略它是安全的。警告的出现是有原因的。@shmose

我使用SpringBoot.yaml进行配置,并创建了配置类的层次结构,以便其他应用程序可以定义其他属性。配置本身具有层次结构


为了保持它的扩展性,我使用了
方法
getsubsubclefs()
的问题是
subclef.conf
类型为
conf,假设您有一个
Map
和一个
Map
。你能在前者中插入一个
子文件夹吗?后者?那么
SubSubConf
呢?如果您仔细考虑过,那么原因应该非常清楚。@MattiaDinosaur For
泛型只在编译时存在。这并不意味着忽略它是安全的。警告的出现是有原因的。@shmosel关于提供的示例,您能更具体一些吗?因为我自己可以对每个一般性的相关问题写下如此可怜的陈述。)@gavenkoa试着回答我第一次评论中的问题,你就有了你的例子。我假设英语不是你的第一语言,所以你可能不知道,但“可怜”是一个不必要的冒犯性和侵略性的词。
public static class Conf<S> {
    public String name;
    public Map<String, S> subConfs;
}

public static class SubConf {
    public String name;
    public Conf<? extends SubConf> conf;
}

public static Map<String, SubConf> getSubConfs(SubConf subconf) {
    // incompatible types: java.util.Map<java.lang.String,capture#1 of ? extends App.SubConf>
    // cannot be converted to java.util.Map<java.lang.String,App.SubConf>
    return subconf.conf.subConfs;
}

public static void main(String[] args) {
    Conf<SubConf> cfg = new Conf<>();
    cfg.name = "cfg";
    SubConf subcfg = new SubConf();
    subcfg.name = "subcfg";
    subcfg.conf = cfg;
    cfg.subConfs = new HashMap<>();
    cfg.subConfs.put(subcfg.name, subcfg);

    Map<String, SubConf> subConfs = getSubConfs(subcfg);
    System.out.println(subConfs.get("subcfg").name);
}
public static Map<String, ? extends SubConf> getSubConfs2(SubConf subconf) {
    Conf<? extends SubConf> cfg = subconf.conf;
    return cfg.subConfs;
}
public static Map<String, SubConf> getSubConfs(SubConf subconf) {
    Conf<? extends SubConf> cfg = subconf.conf;
    return (Map<String, SubConf>) cfg.subConfs;
}
warning: [unchecked] unchecked cast
    return (Map<String, SubConf>) cfg.subConfs;
required: Map<String,SubConf>
found:    Map<String,CAP#1>
where CAP#1 is a fresh type-variable:
  CAP#1 extends SubConf from capture of ? extends SubConf
// Assuming SubConf2 and SubConf3 both extend SubConf ...
HashMap<String, SubConf2> map = new HashMap<>();
Map<String, ? extends SubConf> subconfMap = map;
// This doesn't work, because then you could call map.get("key") 
// and get SubConf3 which is not compatible with the declared SubConf2
subconfMap.put("key", new SubConf3());

// The problem cannot happen with with Map<String, SubConf>, because
// the following statement causes compile time error:
Map<String, SubConf> subconfMap = new HashMap<String, SubConf2>();