Method return type contains subclass definition

会有一股神秘感。 提交于 2020-01-25 03:41:11

问题


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:


  1. The return type is double[][]. The <A extends B> part is the parametrization of the generic method
  2. The restriction extends B implies that only generic types A extending or actually implementing B are allowed in this method.
  3. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!