问题
I come up with a code that uses a syntax like this:
public <A extends B> double[][] foo(C arg) {
.... }
I get a couple of questions by viewing it.
a) The return type of foo(C arg)
is <A extends B> double[][]
? What does this mean? I would understood a return type like double[][]
for example, but I cannot determine what does the previous modifier (maybe?) <A extends B>
does?
b) Why there is a subclass declaration inside a return type? Since A is a subclass of B where do we override or add any methods/members etc? To me it seems that it's a subclass containing just the same methods/members of the base class A. Isn't so? So is there any difference in writing public <A> double[][] foo(C arg)
?
c) Finally I suppose that <> have to do with Java generics but even then I have seen declaration like D<T>
which T is used to parametrize the raw type D
. Here I tried to remove the <> (since I don't understand what they stand for) but compiler complains.
回答1:
- The return type is
double[][]
. The<A extends B>
part is the parametrization of the generic method - The restriction
extends B
implies that only generic typesA
extending or actually implementingB
are allowed in this method. - Cannot answer your third question as it would require the code in the method's body and more context.
来源:https://stackoverflow.com/questions/23425328/method-return-type-contains-subclass-definition