Java generics with class and nested static interface

后端 未结 2 1518
终归单人心
终归单人心 2020-12-09 20:32

I want to use a generic class inside a nested static interface. My objective is to do something like this:

public class MyClass{
    private MyInter         


        
2条回答
  •  孤街浪徒
    2020-12-09 21:25

    A static nested class or nested interface (which is always static, by the way) has no relation to its outer class (or interface) apart from namespace nesting and access to private variables.

    So, the type parameter of the outer class is not available inside the nested interface in your case, you should define it again. To avoid confusion, I recommend using a different name for this inner parameter.

    (As an example in the standard API, look for the interface Map.Entry, nested inside the interface Map, yet has no access to its type parameters and needs to declare them again.)

提交回复
热议问题