Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

我有个类希望泛型 extends 两个或多个类,百度查到 <T extends TestA & TestB> 这种 & 的用法,如:

https://blog.csdn.net/qq_4368...

https://blog.csdn.net/caolaos...

照这么写了 IDE 提示错误,把 jdk 版本从 1.8 改成 11 依然提示错误。直接编译还是提示语法错误,jdk 版本 11。

然后我想是不是 TestA 和 TestB 必须有个共同的父类,试了如下代码:

    public static class TestX {
        
    }
    
    public static class TestA extends TestX {
        
    }
    
    public static class TestB extends TestX {
        
    }
    
    public static class Test<T extends TestA & TestB> {
        
    }

依然编译错误。采用 Maven 编译,只给了个错误链接 http://cwiki.apache.org/confl... 没能发现有用的信息。

想请教一下,到底有没有这个语法?如果有,在哪个版本加入的,能提供一下官方的文档就更好了。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.6k views
Welcome To Ask or Share your Answers For Others

1 Answer

哦,没看仔细,这个 <T extends A & B & C> 语法里,第一个是类,& 后面的只能是接口。我 SB 了,Sorry!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...