简体   繁体   English

接口中的Java泛型子类型

[英]java generics subtype in interface

If I have the following interface and I want to implement it 如果我具有以下接口并且想要实现它

public interface A<E extends Comparable<E>>{
    //code
}

What is the correct syntax for the implementing class declaration? 实现类声明的正确语法是什么? I get an error when I do this 执行此操作时出现错误

public class B<E extends Comparable<E>> implements A<E extends Comparable<E>>{}

Should it just read implements A<E> or just implements A ? 它应该只读取implements A<E>还是仅implements A

Where the Comparable type E is, for example, String , you would want: 在可Comparable类型E是例如String ,您需要:

public class B implements A<String> { ... }

Where you want to retain the generic type parameter declaration in B , you would have: 要在B保留泛型类型参数声明的位置,您将具有:

public static class B<E extends Comparable<E>> implements A<E> { ... }

Note that the E in A is not related to the E in B , ie the following is valid: 需要注意的是EA不相关的EB ,即以下是有效的:

public static class B<Foo extends Comparable<Foo>> implements A<Foo> { ... }

(whether you want to distinguish or not in your code I don't know, but it might help in understanding) (您是否想在我不知道的代码中加以区分,但这可能有助于理解)

public class B<E extends Comparable<E>> implements A<E>{}

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

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