I can neither compile nor refactor the following code to work:
public class DoubleQuestionMark {
public static class Paired1
{
}
public static class Paired2
{
}
public static class Paired3
{
}
// regard it as third party method, not allowed to change signature
public static void perform(Paired1, Paired3> argument) {
}
public static void main(String[] args) {
Paired2 p2 = new Paired2<>();
Paired3 p3 = new Paired3<>();
Paired1, Paired3> p1 = new Paired1<>();
perform(p1); // cannot be applied to given class
}
}
How to prepare an object, acceptable by perform method?
解决方案
You are trying to pass two different types for the same type parameter S.
Paired1, Paired3> Both S must be of the same type:
So this will work:
Paired1, Paired3> p1 = new Paired1<>();
perform(p1);
But this will not:
Paired1, Paired3> p1 = new Paired1<>();
perform(p1);
If you need to pass two different numeric types, both Integer and Double extend Number. So you can use:
Paired1, Paired3> p1 = new Paired1<>();