开发者

For java,how to make sure inner interface and outer interface have same generic type?

Version 1

public interface Outer<T>{
    public interface Inner<T>{
        T get();
        void set(T t);
    }
}

Although same generic type syntax ,these two type are totally independent.

Version 2

public interface Outer<T>{
    public interface Inner<V extends T>{
        V get();
        void set(V t);
    }
}

Got an error:"Cannot make a static reference to the non-static type T"

Version 3

public interface Outer<T>{
    public interface Inner{
       <T> T get();
   开发者_JAVA百科    <T> void set(T m);
    }
}

Not sure whether this is what I want, but seems fine (no errors in eclipse), so I try to implement it:

public Test implements interface Outer.Inner {
       //no problem in these two
       <T> T get(){..};
       <T> void set(T m){...};

        //Errors come up here:
       Map<String,T> map;
       public Test(Map<String,T> map){
            this.map=map
       }

}

the error comes up: "T cannot be resolved to a type" in both

  1. declaration Map<String,T> map
  2. constructor argument public Test(Map<String,T> map){}

so,how can I solve this problem?


Firstly, there is no way to do this, since the outer interface is nothing more than a "namespace" for the inner interface. They really have nothing else to do with each other.

This begs the question: why would you need to have such a control over things? If, in the class which implements the interface, it is decided that using the same type makes sense, then it will be so for that class. But another implementation could, and should be allowed to, choose otherwise. This is especially true since classes which implement Outer and those implementing Inner are not restricted to being nested with each other.


The source of your problem is this ; Inner interface is static , therefore it is not related any instanceof Outer interface. Therefore you can not use, Outer interface generic type for Inner interface..

According to JLS ;

9.5 Member Type Declarations
Interfaces may contain member type declarations (§8.5). A member type declaration in an interface is implicitly static and public


What you should do is, possibly, have Inner extends Outer, if the type must absolutely be enforced, but of course, you'll have to implement methods of both Inner and Outer. I'm not sure you want this.

interface Outer<T> {
  interface Inner<T> extends Outer<T> {
    T myMethod(T t);
  }
}

There isn't really another way to enforce what you want. The reason is that an interface is always static. Also, you don't really want/need this behavior: an interface is already flexible enough for you. Just learn where you may have control over this and the world will be yours!


Error in implementation - you foget to add generic type, this should help:

public class Test<T> implements interface Outer<T>.Inner {

   T get(){..};
   void set(T m){...};

    //Errors come up here:
   Map<String,T> map;
   public Test(Map<String,T> map){
        this.map=map
   }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜