Forcing Java generic parameters to be of the same type

前端 未结 2 1892
闹比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:15

    Keeping in mind the PECS rule, and given the way you are using X, you should be specifying as a lower instead of upper bound:

    void foo(A a, X x)
    

    This way no compiler errors are produced, and you have the most general signature applicable.

提交回复
热议问题