Method signature that takes any parameter as input
How can I declare a method signature that takes Long
, In开发者_如何学JAVAteger
as input?
I tried following but it gives a compilation error:
List<Myobject> fun ( ? extends Number) value){
//impl
}
public <N extends Number>List<T> xx(N a) {...}
This method takes any Number as input:
private <N extends Number> void foo(N param) {}
This one also returns List of the same type
private <N extends Number> List<N> foo(N param) {}
Why not simply declare your argument as Number?
List<Myobject> fun (Number value){
//impl
}
<N extends Number> List<MyObject> fun(N value) { ... }
But in this use-case, I don't really see the benefit of using generics. I mean, why doesn't your method simply take a Number
as argument ?
Simply
private <T extends Number> List<Myobject> fun(T value) {
// do anything with the value
return aMyobjectList;
}
精彩评论