Forcing Java generic parameters to be of the same type

前端 未结 2 1869
闹比i
闹比i 2021-01-04 07:54

How can a similar functionality be achieved without errors?

class A {
   void f(K x) {}
}

void foo(A a, X x) {
    a.f(x); // AN         


        
2条回答
  •  执笔经年
    2021-01-04 08:30

    You can force the parameters to be of the same type by doing the following:

    // the first class, A:
    class A {
      void f(K x) {}
    }
    
    // the second class, defining the method with generic type parameters
    class Test {
       void foo(A a, T x) {
        a.f(x); // now it works!
      }
    }
    
    // a third class, that uses the above two:
    class Main {
      public static void main(final String... args) {
        final Test test = new Test();
        final A a = new A<>();
        test.foo(a, "bar");
      }
    }
    

    What this does is: the method foo defines a generic type parameter T and uses it to enforce that the K type parameter of the class A must match the type of x, the second parameter of foo.

    You could even impose restrictions on if you wish and if it makes sense for your problem, such as void foo(A a, T x) {...}, or with super. You would want this if, as Joni asked on a comment in the question, X is actually a type and not a type parameter: you'd use void foo(...).


    After you've shown more code, the problem becomes clear.

    The method .get() of the container returns an instance of A. Therefore, the type parameter of the instance you obtain from .get() is not fully specified. Usually, it is not very good design to return such an unspecified type. For a video presentation with Joshua Bloch, the author of Effective Java and many APIs and features in Java, showing how to improve such an API, check this: http://www.youtube.com/watch?v=V1vQf4qyMXg&feature=youtu.be&t=22m. At exactly 25'36", Joshua Bloch says "don't try to use them [wildcard types] on return values", and he explains it later. Basically, you don't get any more flexibility by using them, and just makes it painfully hard for users of the API to deal with it (you just felt the effects of doing it...).

    To fix, you could simply try to change the signature of .get() to A get(), so the container class would be:

    public class Container {
      A get() {
        return new A();
      }
    }
    

    Since you do know that get() is returning an instance of A, there's no reason to use the older signature: it simply makes you lose information you already know!

    And if this still doesn't work, your problem might be somewhere else, and you'd need to show even more code... or better still, ask other questions! :)

提交回复
热议问题